带时间序列d的Pandas堆积条形图

2024-04-24 08:33:11 发布

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

我试图用时间序列数据在熊猫身上创建一个堆积条形图:

        DATE        TYPE    VOL
    0   2010-01-01  Heavy   932.612903
    1   2010-01-01  Light   370.612903
    2   2010-01-01  Medium  569.451613
    3   2010-02-01  Heavy   1068.250000
    4   2010-02-01  Light   341.535714
    5   2010-02-01  Medium  484.250000
    6   2010-03-01  Heavy   1090.903226
    7   2010-03-01  Light   314.419355

X=日期,Y=体积,堆叠=类型

非常感谢任何帮助,谢谢。在


Tags: 数据类型datetype时间体积序列light
1条回答
网友
1楼 · 发布于 2024-04-24 08:33:11

我们用熊猫的情节:

df = df.set_index('DATE')
#moved the 'DATE' column into the index

df.index = pd.to_datetime(df.index)
#convert the string 'DATE' column to a datetime dtype

df.set_index('TYPE',append=True)['VOL'].unstack().plot.bar(stacked=True,figsize=(10,8))
#Moved 'TYPE' into the index with 'DATE' then unstacked 'TYPE' to create a dataframe that has 'DATE' as row labels and 'TYPE' as column labels.  And, then used pandas dataframe plot to chart that frame as a vertical bar chart with stacked=True.

enter image description here

相关问题 更多 >