我有一个数据框架,其中包含一年中每小时测量6种不同气象变量的数据。我试图创建子图,以显示同一子图中每个气象数据的每小时和每日平均值。但是,我还没有接近。
这样我就得到了每个变量的子图:
sns.set(style = "white", palette = "Dark2")
yaxis_titles = ['Global Irradiance $(W/m^2)$', 'Precipitation (-)', 'Relative Humidity (%)', 'Ambient temperature (°C)', 'Wind speed (m/s)', 'Wind speed direction (°)']
axes = meteo.plot( figsize=(12, 20), subplots=True, sharex = True, title = yaxis_titles)
我试图做这样的事情,因为我正在创建每个循环的绘图,所以让我整理了想要的东西,但是我不确定如何使它们共享x轴,这样就不会详细。
fig, ax = plt.subplots(1, len(meteo.columns), sharex=True, tight_layout=True)
for i in range(len(meteo.columns)):
s = 611+i
ax = plt.subplot(s) # s= nrows, ncols, index
meteo[meteo.columns[i]].plot(title = yaxis_titles[i], label = "Hourly")
meteo[meteo.columns[i]].resample("D").mean().plot(title = yaxis_titles[i], ax= ax, label = "Daily average")
有人能指出我正确的方向吗?谢谢!
编辑:
我找到了一种方法来做,但是由于传说显示的不正确,它还不是很完美。实际上,我不希望完全不显示它们,或者仅希望指出黑色的“-”线是每日平均值。
import matplotlib.lines as mlines
yaxis_titles = ['Global Irradiance $(W/m^2)$', 'Precipitation (-)', 'Relative Humidity (%)', 'Ambient temperature (°C)', 'Wind speed (m/s)', 'Wind speed direction (°)']
meteo_mean = meteocorrect.resample("D").mean()
hourly = meteo.plot(figsize=(12, 20), subplots=True, sharex = True, title = yaxis_titles, label = None)
daily = meteo_mean.plot(subplots = True, ax = hourly, color='black', style = "--", label = None);
black_line = mlines.Line2D([], [], color='black',
markersize=15, label='Daily Average')
plt.legend(handles=[black_line]);
它给出了这样的内容:
问题是,对于“ black_line”,我没有设法使其变为虚线,即使我在子图中将图例设置为“无”,它们仍然会出现。
我想你很亲密。我可以看到的主要问题是您没有直接利用循环之前创建的Axes对象。
我将直接遍历这些列和您的数据框列,并始终将Axes对象传递给数据框的plot
方法。
fig, axes = plt.subplots(len(meteo.columns), 1, sharex=True, tight_layout=True)
for ax, col, title in zip(axes.flat, meteo.columns, yaxis_title):
meteo.loc[:, col].plot(title=title, label="Hourly", ax=ax)
meteo.loc[:, col].resample("D").mean().plot(title=titles, ax=ax, label="Daily average")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句