【问题标题】:gap in timeseries plot时间序列图中的间隙
【发布时间】:2021-08-25 22:12:36
【问题描述】:

我有一年的数据,我想绘制它们的季节性模式。所以我只是为每个季节创建了子数据。但我的冬季数据图有差距。它不能按顺序绘制三个月。

这是我的数据:

winter = pd.concat([countData19_gdf.loc['2019-12-01':'2019-12-31'], countData19_gdf.loc['2019-01-01':'2019-02-28']])
winter= winter.sort_index()
min_count = countData19_gdf['volume'].min()
max_count = countData19_gdf['volume'].max() + 20


fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(16,10))


line_width = 2
ax[0,0].plot(winter.resample('d').mean()['volume'].index, winter.resample('d').mean()['volume'], c='blue', lw=line_width);
ax[0,1].plot(countData19_gdf.loc['2019-03-01': '2019-05-31'].resample('d').mean()['volume'].index, countData19_gdf.loc['2019-03-01': '2019-05-31'].resample('d').mean()['volume'] ,c='orange',lw=line_width);
ax[1,0].plot(countData19_gdf.loc['2019-06-01': '2019-08-31'].resample('d').mean()['volume'].index, countData19_gdf.loc['2019-06-01': '2019-08-31'].resample('d').mean()['volume'], c='green', lw=line_width);
ax[1,1].plot(countData19_gdf.loc['2019-09-01': '2019-11-30'].resample('d').mean()['volume'].index, countData19_gdf.loc['2019-09-01': '2019-11-30'].resample('d').mean()['volume'], c='brown', lw=line_width);

ax[0,0].title.set_text('Winter')
ax[0,1].title.set_text('Spring')
ax[1,0].title.set_text('Summer')
ax[1,1].title.set_text('Fall')
for ax in [ax[0,1], ax[1,0], ax[1,1]]:
    # Set minor ticks with day numbers
    ax.xaxis.set_minor_locator(dates.DayLocator(interval=10))
    ax.xaxis.set_minor_formatter(dates.DateFormatter('%d'))
    # Set major ticks with month names
    ax.xaxis.set_major_locator(dates.MonthLocator())
    ax.xaxis.set_major_formatter(dates.DateFormatter('\n%b'))
plt.savefig('seasonal_global.png')
plt.show()

【问题讨论】:

    标签: python pandas matplotlib time-series geopandas


    【解决方案1】:

    由于您显示了两个不同冬季的冬季月份,一个从 2018 年开始到 2019 年结束,另一个从 2019 年开始到 2020 年结束,因此出现了图中的空白。

    您需要对数据进行子集化,以便收集适当的月份,如下代码所示:

    import numpy as np
    import pandas as pd
    
    np.random.seed(42)
    
    datetime_index = pd.date_range(start='2018-01-01', end='2020-12-31')
    
    volume = np.random.randint(low=30, high=60, size=datetime_index.shape[0])
    
    data = pd.DataFrame({'volume': volume},
                        index=datetime_index)
    
    winter = data['2019-12':'2020-02']
    
    winter.plot()
    

    它绘制了这个:

    如果你没有超过一年的数据,那么你可以用浅灰色的其他季节来填补空白,如下图:

    fig, ax = plt.subplots(1, 1, figsize=(16,10))
    line_width = 2
    ax.plot(data['volume'], c='grey', lw=line_width, label='All year')
    ax.plot(data[:'2019-02'], c='blue', lw=line_width, label='Winter')
    ax.plot(data['2019-12':], c='blue', lw=line_width)
    plt.legend()
    plt.title('Volume across 2019')
    plt.xlabel('Month')
    plt.ylabel('Volume')
    plt.show()
    

    我使用的合成数据比真实数据更不稳定。您可以使用 rolling() 使用移动平均线平滑时间序列,以提高随时间变化的可读性。

    【讨论】:

    • 我只有 2019 年的数据。我的数据集中没有 2020 年的数据
    • 您可以将它们全部绘制在同一轴上,并按季节对线条进行颜色编码,以避免出现差距。
    • 感谢 Paulo Schau Guerra。我的问题是我是 python 新手,事情需要很多时间。我会试试你的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2016-11-29
    • 2017-04-05
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多