Python-按月和日(忽略年份)绘制多个数据框

时间:2019-03-09 14:19:48

标签: python-3.x pandas dataframe matplotlib plot

我有多个具有不同年份数据的数据框。

数据框中的数据是:

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

我试图将所有这些数据帧都绘制在单个图形(图中)中,是的,我做到了,但这不是我想要的。

我使用以下代码绘制了数据框

>>> for p in its:
    plt.plot(p.index,p.values)
>>> plt.show()

我得到下图 Graph i Got by Combing all dfs

但这不是我想要的 我希望图像这样 Graph i want

简单,我希望图形忽略年份并按月和日进行绘制

1 个答案:

答案 0 :(得分:1)

您可以尝试根据月份,日期和图表将日期时间索引转换为时间序列整数

df3 = pd.concat(its,axis=1)
xindex= df3.index.month*30 + df3.index.day
plt.plot(xindex,df3)
plt.show()

如果您希望获取日期时间信息而不是整数,可以将xticks添加到框架

labels = (df3.index.month*30).astype(str)+"-" + df3.index.day.astype(str)
plt.xticks(df3.index.month*30 + df3.index.day, labels)
plt.show()
相关问题