python - ValueError:缓冲区的双轴图表的维数错误(预期为 1,得到 2)
问题描述
我正在尝试绘制一个双轴图表,其中一年中的月份是 2 列共享的 x 轴,2 个不同的 y 轴应该是不同列的值。数据框,df 看起来像这样
index TotalEvtCount ObservedNumEvntNam
Mar 2670 71
Apr 3330 55
Jun 7500 65
...
这是代码:
t = df.index
data1=df['TotalEvtCount']
data2=df['ObservedNumEvntNam']
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('month')
ax1.set_ylabel('EvtCount', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:blue'
ax2.set_ylabel('NumEvntNam', color=color) # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show()
但是我得到了这个 ValueError: Buffer has wrong number of dimensions (expected 1, got 2)