我试图绘制一个双轴图表,其中一年中的月份是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错误:缓冲区的维数错误(预期为1,为2)