【问题标题】:Plot subplots of different hourly time series: plot daily average on each hourly subplots绘制不同小时时间序列的子图:在每个小时子图上绘制日平均值
【发布时间】: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


    【解决方案1】:

    我认为你很接近。我可以看到的主要问题是您没有直接利用循环之前创建的 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")
    

    【讨论】:

    • YEEEEIH,非常感谢!我很挣扎!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2021-03-22
    相关资源
    最近更新 更多