绘制时间序列图(Matplotlib)

使用Matplotlib绘制时间序列图时,横轴的时间数量与纵轴的数据个数一致是非常关键的

实际处理数据过程中,可能会出现时间戳重复,导致纵轴数据重复的情况。
For example:

flow = df['flow'].values
flow_dates = np.arange('2019-03-10', '2019-04-11', dtype='datetime64[h]')
flow_loc = mdates.WeekdayLocator(byweekday=mdates.WE)
flow_fmt = mdates.DateFormatter('%m-%d')

len(flow) must be equal to len(flow_dates). If these are not equal, then flow_loc will fail to plot the time series in the following code.

num_forecast_steps = 24 * 7 * 2 # Two weeks.
flow_training_data = flow[:-num_forecast_steps]
fig = plt.figure(figsize=(12, 3))
ax = fig.add_subplot(1, 1, 1)
ax.plot(flow_dates[:-num_forecast_steps],
        flow[:-num_forecast_steps], lw=2, label="training data")
ax.set_ylabel("Hourly flow (CMH)")
ax.xaxis.set_major_locator(flow_loc)
ax.xaxis.set_major_formatter(flow_fmt)
fig.autofmt_xdate()
plt.show()

绘制时间序列图(Matplotlib)_第1张图片

你可能感兴趣的:(Matplotlib)