I've a dataframe like this:
DATE | VALUE | TYPE |
---|---|---|
2021-01-11 | 57 | A |
2021-02-11 | 34 | B |
2021-03-11 | 43 | A |
2021-04-11 | 15 | B |
... |
My question is how I could plot a bar graph, the mean monthly ordered by date of course and grouped by 'TYPE'
I'm using Pandas with this extract of code:
df = df.set_index('DATE')
df.index = pd.to_datetime(df.index)
df = df.resample('M').mean()
df.plot(kind='bar',stacked=True)
I want to draw a stacked bar plot but I don't know how...
CodePudding user response:
Not sure if I understand correctly but if you want to stack values by types with date as x-axis, I would use pivot (do not set index first):
df = df.pivot('DATE', 'TYPE', 'VALUE')
df.plot(kind='bar', stacked=True, rot=0)
plt.show()
With the slightly edited table to show the stacking:
DATE | VALUE | TYPE |
---|---|---|
2021-01-11 | 57 | A |
2021-02-11 | 34 | A |
2021-02-11 | 12 | B |
2021-03-11 | 43 | A |
2021-04-11 | 15 | B |
You get the following: