I have a Dataframe as given below
df = pd.DataFrame(
{
'Dates': ['2021-04-11', '2021-06-08', '2021-06-08', '2021-06-09', '2021-06-10', '2021-07-18', '2021-07-18'],
'Results': ['Negative', 'Invalid', 'Negative','Negative','Negative', 'Negative', 'Positive' ],
'Size': [1, 1, 1, 1, 4, 21, 2]
})
I want to plot a subdivided bar graph as shown below in the given picture. [![enter image description here][1]][1]
I have been stuck on this for a while now. Tried using groupby(), sum(), size() etc, but could not figure it.
Any help is greatly appreciated.
Thanks. [1]: https://i.sstatic.net/HrKRY.jpg
Just reshape it by setting indices on date and results, then unstacking Results
. After that, plot.
df.set_index(['Dates', 'Results']).sort_index(0).unstack().plot.bar(stacked=True)
If you don't want it with a column multi-index, and instead want "Invalid", "Negative", "Positive", just create an intermediate data frame from the unstack
(I'll call it temp
) and assign temp.columns = temp.columns.get_level_values(1)
. Then call temp.plot.bar(stacked=True)
.
To specify size using the OOP matplotlib
interface:
f, ax = plt.subplots(figsize=(HEIGHT, WIDTH))
temp.plot.bar(ax=ax, stacked=True)
Save the figure to file using f.savefig(PATH)
.