Python-按月和日(忽略年份)绘制多个数据框 [英] Python - Plot Multiple Dataframes by Month and Day (Ignore Year)
问题描述
我有多个具有不同年份数据的数据框.
I have multiple dataframes having different years data.
数据框中的数据是:
>>> its[0].head(5)
Crocs
date
2017-01-01 46
2017-01-08 45
2017-01-15 43
2017-01-22 43
2017-01-29 41
>>> its[1].head(5)
Crocs
date
2018-01-07 23
2018-01-14 21
2018-01-21 23
2018-01-28 21
2018-02-04 25
>>> its[2].head(5)
Crocs
date
2019-01-06 90
2019-01-13 79
2019-01-20 82
2019-01-27 82
2019-02-03 81
我尝试将所有这些数据框绘制在单个图形(图形)中,是的,我完成了,但这不是我想要的.
I tried to plot all these dataframes in single figure (graph), yeah i accomplished but it was not what i wanted.
我使用以下代码绘制了数据框
I plotted the dataframes using the following code
>>> for p in its:
plt.plot(p.index,p.values)
>>> plt.show()
我得到下图
但这不是我想要的我希望图表是这样的
but this is not what i wanted i want the graph to be like this
很简单,我希望图形忽略年份并按月和日进行绘制
Simply i want graph to ignore years and plot by month and days
推荐答案
您可以尝试将日期时间索引转换为基于月份和日期以及绘图的时间序列整数
You can try of converting the datetime index to timeseries integers based on month and date and plot
df3 = pd.concat(its,axis=1)
xindex= df3.index.month*30 + df3.index.day
plt.plot(xindex,df3)
plt.show()
如果你想要日期时间信息而不是整数,你可以在框架中添加 xticks
If you want to have datetime information than integers you can add xticks to frame
labels = (df3.index.month*30).astype(str)+"-" + df3.index.day.astype(str)
plt.xticks(df3.index.month*30 + df3.index.day, labels)
plt.show()
这篇关于Python-按月和日(忽略年份)绘制多个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!