时间序列图中的间隙

2024-06-16 11:26:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

以下是我的数据:

enter image description here

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()

enter image description here


Tags: 数据indexplotlineaxmeanwidthloc
1条回答
网友
1楼 · 发布于 2024-06-16 11:26:15

图中出现的间隙是因为显示了两个不同冬季的冬季月份,一个始于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()

它描绘了这一点:

Volume in winter 2019/20

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

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()

Plot of volume across the year of 2019

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

相关问题 更多 >