Python - Plot Multiple Dataframes by Month and Day (Ignore Year)

不羁的心 提交于 2021-01-21 09:29:06

问题


I have multiple dataframes having different years data.

The data in dataframes are:

>>> 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()

and i got the following graph

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


回答1:


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()

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()


来源:https://stackoverflow.com/questions/55078265/python-plot-multiple-dataframes-by-month-and-day-ignore-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!