问题参考 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,
- call
plt.plot(...)
to plot to the current axes. Set the current axes withplt.sca()
, or - 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])