本篇通过一个简单的demo展示:如何使用matplotlib包 绘制时间序列图。
一、读取数据及处理
- 通过pandas读取CSV文件。
- 查看原数据信息,原数据时间范围 2022-10-15 0:00:00 ~ 2022-10-20 0:00:00,原数据一共六天(864条数据),时间粒度10min,每天144条数据。
二、绘制图形
# 图片大小设置
fig = plt.figure(figsize=(30,10), dpi=100)
ax = fig.add_subplot(111)
# 生成时间序列:X轴刻度数据
table = pd.DataFrame([i for i in range(864)],columns=['value'],index=pd.date_range('2022-10-15 0:00:00', '2022-10-20 23:50:00', freq='10min'))
# X轴时间刻度格式 & 刻度显示
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.xticks(pd.date_range(table.index[0],table.index[-1],freq='D'), rotation=45)
# 绘图
ax.plot(table.index,df['prediction'],color='darkorange', label='y_pred')
ax.plot(table.index,df['label'],color='steelblue', label='lable')
ax.legend()
plt.gcf().autofmt_xdate()
plt.show()
三、效果图如下