我有两个numpy.ndarrays
,bcmonthly
和dailyavg
。你知道吗
bcmonthly has a length of 12 and shape of (12,)
dailyavg has a length of 364 and shape of (364,)
bcmonthy
是月平均值,dailyavg
是日平均值。我想画12个月的坐标轴。你知道吗
绘图bcmonthly
没有问题,因为它的形状是12。但是,当我同时绘制dailyavg
时,我得到以下错误:
ValueError: x and y must have same first dimension, but have shapes (12,) and (364,)
下面是我的代码:
fig = plt.figure()
ax1=fig.add_subplot(111)
ax1.plot(months,bcmonthly,'r') #months is a list months=['jan','feb',..etc]
ax2 = ax1.twinx()
ax2.plot(months, dailyavg)
plt.show()
如果要将日平均数与月平均数绘制在同一个图上,则可以更轻松地构造两个数组,并根据天数数组绘制它们,然后自己处理标记。像这样的
这给了你![Daily and Monthly averages](https://i.stack.imgur.com/TO6dL.png)
或者,您可以使用面向对象的方法
ax.plot()
,但这需要您分别指定记号标签和位置当绘制
months
与dailyavg
时,您需要扩展months
使其长度为364 Matplotlib不能为您决定months
中的12个x值中的哪一个分配364个日平均值,但是您可以自己通过制作一个适当长度的x值列表来提供该信息。你知道吗所以,在本例中,这似乎意味着创建一个包含
"January"
31次,然后"February"
28次的列表,依此类推。。。直到长度达到364(取决于错过哪一天?)你知道吗相关问题 更多 >
编程相关推荐