Pandas - Stacked Bar Chart With Timeseries Data
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.61
Solution 1:
Let's use pandas plot:
df = df.set_index('DATE')
#moved the 'DATE'columninto the index
df.index = pd.to_datetime(df.index)
#convert the string 'DATE'columnto 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'tocreate a dataframe that has 'DATE'asrow labels and'TYPE'ascolumn labels. And, then used pandas dataframe plot to chart that frame as a vertical bar chart with stacked=True.
Post a Comment for "Pandas - Stacked Bar Chart With Timeseries Data"