pandas -带有时间序列数据的堆积条形图 [英] pandas - stacked bar chart with timeseries data

查看:51
本文介绍了 pandas -带有时间序列数据的堆积条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用时间序列数据在熊猫中创建堆叠的条形图:

I'm trying to create a stacked bar chart in pandas using time series data:

        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 = vol,堆栈 = 类型

X = date, Y = vol, stacks = type

非常感谢任何帮助,谢谢.

any help greatly appreciated, thankyou.

推荐答案

让我们使用熊猫图:

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.

这篇关于 pandas -带有时间序列数据的堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆