【发布时间】:2020-12-04 01:31:10
【问题描述】:
我有一个数据框,其中包含一年中 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”我没有设法使它变成虚线,即使我在子图中设置了 legend = None,它们仍然出现。
【问题讨论】:
标签: python matplotlib time-series