plt.plot() 无法使用参数ax

问题参考 TypeError: inner() got multiple values for keyword argument 'ax'

fig, ax=plt.subplots(2,1)
plt.plot(a, b, 'go-', label='line 1', linewidth=2, ax=ax) # 会报错

这时因为ax不是plt.plot()的一个有效的参数。原因是plt.plot()会调用当前活跃的axes的plot方法,和plt.gca().plot()是相同的。因此在调用时axes就被给定了。

Solution: Don't use ax as argument to plt.plot(). Instead,

  1. call plt.plot(...) to plot to the current axes. Set the current axes with plt.sca(), or
  2. directly call the plot() method of the axes. ax.plot(...)

Mind that in the example from the question, ax is not an axes. If this is confusing, name it differently,

fig, ax_arr = plt.subplots(2,1)

ax_arr[0].plot(a, b, 'go-', label='line 1', linewidth=2)
ax_arr[0].set_xticks(a)
ax_arr[0].set_xticklabels(list(map(str,a)))

df.plot(kind='bar', ax=ax_arr[1])
原文地址:https://www.cnblogs.com/ZeroTensor/p/10409240.html