对于时间序列的生成,使用datetime和pandas
这两个包:
具体思路如下:
import pandas as pd
import datetime# 创建起始年份和结束年份
start_year = 2008
end_year = 2022# 创建每年6-10月1日的时间序列
dates = pd.date_range(start=f'{start_year}-06-01', end=f'{end_year}-10-01', freq='MS')# 筛选出每年的6-10月1日
filtered_dates = [date for date in dates if date.month in [6, 7, 8, 9, 10]]# 将时间序列格式化为字符串并存储在标签列表中
labels = [d.strftime('%Y-%m-%d') for d in filtered_dates]# 绘制图形并使用标签作为x轴
import matplotlib.pyplot as pltx = range(len(labels))
y = [i**2 for i in x]plt.plot(x, y)
plt.xticks(x, labels, rotation=90)plt.show()
- 间隔太密了,可以做适当修改
plt.xticks(x[::10], labels[::10], rotation=90)
欢迎评论或者加我交流,分享更简单的绘制方法~