Reputation: 29
I would like to create a stack bar graph to see the percentage of each virus by week using seaborn. Below is my dataset:
```
data = {'Week': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2],
'Virus': ['A', 'B', 'C', 'A', 'B', 'C', 'B', 'C', 'A', 'D', 'A', 'A', 'C', 'D', 'B', 'C', 'D', 'B']}
df = pd.DataFrame(data)
```
Every bar should represent one week of data with each virus having a colour of its own and the % on the stacked bar graph itself.
Thank you.
Upvotes: 0
Views: 66
Reputation: 10017
Seaborn itself doesn't provide easy stacked chart creation. Using pandas bar plot and selecting stacked=True
makes it pretty easy.
Below is the code. The data is changed to allow for easy plotting, so that the weeks are in index and each column has the virus names.
data = {'Week': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2],
'Virus': ['A', 'B', 'C', 'A', 'B', 'C', 'B', 'C', 'A', 'D', 'A', 'A', 'C', 'D', 'B', 'C', 'D', 'B']}
df = pd.DataFrame(data)
df=df.groupby(['Virus', 'Week']).size().reset_index().pivot(columns='Virus', index='Week', values=0)
df.plot(kind='bar', stacked=True)
EDIT
As requested in comments have been incorporated.
div
(borrowed code from here)bbox_to_anchor()
to set the position of the legendrot()
to keep the x-axis labels verticaldata = {'Week': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2],
'Virus': ['A', 'B', 'C', 'A', 'B', 'C', 'B', 'C', 'A', 'D', 'A', 'A', 'C', 'D', 'B', 'C', 'D', 'B']}
df = pd.DataFrame(data)
df=df.groupby(['Virus', 'Week']).size().reset_index().pivot(columns='Virus', index='Week', values=0)
cols=df.columns.values
df[cols]=df[cols].div(df[cols].sum(axis=1), axis=0).multiply(100)
fig = plt.figure()
df.plot(kind='bar', stacked=True, rot = 0, ax=fig.gca())
plt.legend(bbox_to_anchor=(1, 1))
Plot
Upvotes: 1