Faithshield
Faithshield

Reputation: 29

Creating Stack Graph

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

Answers (1)

Redox
Redox

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)

enter image description here

EDIT

As requested in comments have been incorporated.

  • To change the values to percentage, you can use div (borrowed code from here)
  • Use bbox_to_anchor() to set the position of the legend
  • Use rot() to keep the x-axis labels vertical
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)
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

enter image description here

Upvotes: 1

Related Questions