繁体   English   中英

不同小时时间序列的 Plot 子图:plot 每个小时子图的日平均值

[英]Plot subplots of different hourly time series: plot daily average on each hourly subplots

我有一个 dataframe 一年中每小时测量 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)

我试图做这样的事情,这让我理清了我想要的东西,但是因为我正在为每个循环创建一个 plot,然后我不知道如何让它们共享 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,它们仍然会出现。

我想你很接近。 我可以看到的主要问题是您没有直接使用您在循环之前创建的 Axes object。

我将直接遍历这些和您的 dataframe 列,并始终将 Axes object 传递给数据帧的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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM