Python绘制365天的年度数据

2024-04-26 06:57:27 发布

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

下面给出了我的原始数据帧的示例数据。原始数据框架有20年的数据

input_df =             
Datetime         Data             
2000-05-31       0.000
2000-06-20       8.204
2000-06-21       7.724
2000-06-22       7.268
2000-06-23       3.687
2017-01-03       2.718
2017-01-04       3.113
2017-01-05       3.841
2017-01-06       4.135
2017-01-07       0.819
2017-01-08       3.537
2017-01-09       3.471

我想画出上面的数据,这样我想得到下面的图,表示一年数据的一种颜色和标记

enter image description here

我试着用我的方法解决,但没有成功。 我的解决方案:

year_group = input_df.groupby(pd.Grouper(freq='A'),axis=1)    
years = pd.DataFrame()
for name, group in year_group:
    years[name.year] = group.values
years.plot(subplots=True, legend=False)
pyplot.show()

Tags: 数据name标记框架示例dfinputdata
1条回答
网友
1楼 · 发布于 2024-04-26 06:57:27

像这样的

# dummy data
dti = pd.date_range('2000-01-01', periods=20*12, freq='M')
df = pd.DataFrame(np.random.random(size=(20*12,)), index=dti, columns=['value'])

import itertools
marker = itertools.cycle(['.', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', '8', 's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd', '|', '_', 'P', 'X'])

for y in range(2000,2020):
    temp = df.loc[df.index.year==y]
    plt.plot(temp.index.dayofyear, temp.value, label=y, marker=next(marker))
plt.legend()

enter image description here

相关问题 更多 >