Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a stacked bar chart for my DataFrame using seaborn [duplicate]

I have a DataFrame df:

df = pd.DataFrame(columns=["App","Feature1", "Feature2","Feature3", "Feature4","Feature5", "Feature6","Feature7","Feature8"], data=[['SHA', 0, 0, 1, 1, 1, 0, 1, 0], ['LHA', 1, 0, 1, 1, 0, 1, 1, 0], ['DRA', 0, 0, 0, 0, 0, 0, 1, 0], ['FRA', 1, 0, 1, 1, 1, 0, 1, 1], ['BRU', 0, 0, 1, 0, 1, 0, 0, 0], ['PAR', 0, 1, 1, 1, 1, 0, 1, 0], ['AER', 0, 0, 1, 1, 0, 1, 1, 0], ['SHE', 0, 0, 0, 1, 0, 0, 1, 0]])  # display(df)    App  Feature1  Feature2  Feature3  Feature4  Feature5  Feature6  Feature7  Feature8 0  SHA         0         0         1         1         1         0         1         0 1  LHA         1         0         1         1         0         1         1         0 2  DRA         0         0         0         0         0         0         1         0 3  FRA         1         0         1         1         1         0         1         1 4  BRU         0         0         1         0         1         0         0         0 5  PAR         0         1         1         1         1         0         1         0 6  AER         0         0         1         1         0         1         1         0 7  SHE         0         0         0         1         0         0         1         0 

I want to create a stacked bar chart so that each stack would correspond to App while the Y axis would contain the count of 1 values and the X axis would be Feature.

It should be similar to this bar chart with the only difference that now I want to see stack bars and a legend with colors:

df_c = df.iloc[:, 1:].eq(1).sum().rename_axis('Feature').reset_index(name='Cou‌nt') df_c = df_c.sort_values('Cou‌nt') plt.figure(figsize=(12,8)) ax = sns.barplot(x="Feature", y='Cou‌nt', data=df_c, palette=sns.color_palette("GnBu", 10)) plt.xticks(rotation='vertical') ax.grid(b=True, which='major', color='#d3d3d3', linewidth=1.0) ax.grid(b=True, which='minor', color='#d3d3d3', linewidth=0.5) plt.show() 

enter image description here

like image 981
Dinosaurius Avatar asked Nov 06 '17 13:11

Dinosaurius


People also ask

How to create a stacked bar chart in Seaborn using PANDAS?

To create a stacked bar chart, we can use Seaborn's barplot () method, i.e., show point estimates and confidence intervals with bars. Create df using Pandas Data Frame.

How do I create a stacked bar plot in Seaborn?

This gives the overall graph a stacked look, with one set of observations placed over the second set. We do not have a function that can create such stacked bar plots directly, but we can use the traditional seaborn.barplot () method to plot two individual bar graphs and place them both on top of each other.

What is a bar chart in Seaborn?

But if you’re new to Seaborn or new to data visualization in Python, you should probably read the full tutorial. Let’s quickly review bar charts. According to Wikipedia, bar charts (AKA, bar plots) are: a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent.

How to create a stacked bar chart in Python?

Given two series of data, Series 1 (“bottom”) and Series 2 (“top”), to create a stacked bar chart you just need to create: 1. Series 3 = Series 1 + Series 2. Once you have Series 3 (“total”), then you can use the overlay feature of matplotlib and Seaborn in order to create your stacked bar chart.


1 Answers

You could use pandas plot as @Bharath suggest:

import seaborn as sns sns.set() df.set_index('App').T.plot(kind='bar', stacked=True) 

Output:

enter image description here

Updated:

from matplotlib.colors import ListedColormap df.set_index('App')\ .reindex_axis(df.set_index('App').sum().sort_values().index, axis=1)\ .T.plot(kind='bar', stacked=True, colormap=ListedColormap(sns.color_palette("GnBu", 10)), figsize=(12,6))

Updated Pandas 0.21.0+ reindex_axis is deprecated, use reindex

from matplotlib.colors import ListedColormap  df.set_index('App')\   .reindex(df.set_index('App').sum().sort_values().index, axis=1)\   .T.plot(kind='bar', stacked=True,           colormap=ListedColormap(sns.color_palette("GnBu", 10)),            figsize=(12,6)) 

Output:

enter image description here

like image 75
Scott Boston Avatar answered Sep 23 '22 23:09

Scott Boston