Python 根据相同的x值绘制两个不同长度的matplotlib列表
我有两个Python 根据相同的x值绘制两个不同长度的matplotlib列表,python,matplotlib,time-series,Python,Matplotlib,Time Series,我有两个numpy.ndarray,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个月的x轴的关系 绘图bcmonthly没有问题,因为它的形状是12。但是,当我同时绘制dailyavg时,我得到以下错误: ValueError:
numpy.ndarray
,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个月的x轴的关系
绘图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()
绘制
months
vsdailyavg
时,您需要延长months
以获得长度364——Matplotlib无法为您决定months
中的12个x值中的哪一个分配364个日平均值,但是您可以通过制作一个适当长度的x值列表来提供这些信息
因此,在本例中,这似乎意味着制作一个包含31次“一月”的列表,然后是28次“二月”,依此类推。。。在达到长度364之前(取决于缺少哪一天?如果要在同一个图上绘制日平均值和月平均值,可能更容易构建两个数组,并根据天数数组绘制它们,然后自己处理标记。像这样的
import matplotlib.pyplot as plt
import numpy as np
bcmonthly = np.random.rand(12) # Creates some random example data,
dailyavg = np.random.rand(365) # use your own data in place of this
days = np.linspace(0, 364, 365)
months = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September',
'October', 'November', 'December']
lmonths = [0, 2, 4, 6, 7, 9, 11]
smonths = [3, 5, 8, 10]
month_idx = list()
idx = -15 # Puts the month avg and label in the center of the month
for jj in range(len(months)):
if jj in lmonths:
idx += 31
month_idx.append(idx)
elif jj in smonths:
idx += 30
month_idx.append(idx)
elif jj == 1:
idx += 28
month_idx.append(idx)
fig = plt.figure(figsize=(10,4), dpi=300)
plt.plot(month_idx,bcmonthly,'r')
plt.plot(days, dailyavg, ':', linewidth=1)
plt.xlim([-1,366])
plt.title("Monthly and Daily Averages")
plt.xticks(month_idx, months, rotation=45)
plt.show()
这给了你
或者,您可以使用面向对象的方法ax.plot()
,但这需要您分别指定记号标签和位置
import matplotlib.pyplot as plt
import numpy as np
bcmonthly = np.random.rand(12)
dailyavg = np.random.rand(365)
days = np.linspace(0, 364, 365)
months = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September',
'October', 'November', 'December']
lmonths = [0, 2, 4, 6, 7, 9, 11]
smonths = [3, 5, 8, 10]
month_idx = list()
idx = -15 # Puts the month avg and label in the center of the month
for jj in range(len(months)):
if jj in lmonths:
idx += 31
month_idx.append(idx)
elif jj in smonths:
idx += 30
month_idx.append(idx)
elif jj == 1:
idx += 28
month_idx.append(idx)
fig = plt.figure(figsize=(10,4), dpi=300)
ax1 = fig.add_subplot(111)
ax1.plot(month_idx,bcmonthly,'r')
ax2 = ax1.twinx()
ax2.plot(days, dailyavg, ':', linewidth=1)
plt.xlim([-1,366])
plt.title("Monthly and Daily Averages")
ax1.set_xticklabels(months, rotation=45)
ax1.set_xticks(month_idx)
plt.show()
重复的?请阅读前一篇文章。感谢您的详细回复。我通过实现来解决它:x1=np.linspace(0,12364)x2=np.linspace(0,12,12)plt.plot(x1,dailyavg,'r')plt.plt(x2,bcmonthly)plt.show()。这似乎解决了问题。嗨,鬼。有没有一种方法可以通过面向对象技术实现这一点。我需要更多的控制。使用ax.plot?