Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn multiple barplots

I have a pandas dataframe that looks like this:

    class       men       woman   children 0   first   0.91468    0.667971   0.660562 1   second  0.30012    0.329380   0.882608 2   third   0.11899    0.189747   0.121259 

How would I create a plot using seaborn that looks like this? Do I have to rearrange my data in some way?


(source: mwaskom at stanford.edu)

like image 562
jakko Avatar asked Aug 06 '16 19:08

jakko


People also ask

How do you plot multiple Barplots in seaborn?

We can make multiple columns of the barplot by using the seaborn function group bar. The groupby() method in Pandas is used to divide data into groups depending on specified criteria. In the following example script, we have included the matplotlib library and seaborn module for plotting multiple columns using barplot.

How do you stack barplot in seaborn?

A stacked Bar plot is a kind of bar graph in which each bar is visually divided into sub bars to represent multiple column data at once. To plot the Stacked Bar plot we need to specify stacked=True in the plot method. We can also pass the list of colors as we needed to color each sub bar in a bar.

How do you put two seaborn plots together?

To create two graphs, we can use nrows=1, ncols=2 with figure size (7, 7). Create a data frame with keys, col1 and col2, using Pandas. Use countplot() to show the counts of observations in each categorical bin using bars. Adjust the padding between and around the subplots.


1 Answers

Yes you need to reshape the DataFrame:

df = pd.melt(df, id_vars="class", var_name="sex", value_name="survival rate") df Out:      class       sex  survival rate 0   first       men       0.914680 1  second       men       0.300120 2   third       men       0.118990 3   first     woman       0.667971 4  second     woman       0.329380 5   third     woman       0.189747 6   first  children       0.660562 7  second  children       0.882608 8   third  children       0.121259 

Now, you can use factorplot (v0.8.1 or earlier):

sns.factorplot(x='class', y='survival rate', hue='sex', data=df, kind='bar') 

enter image description here

For versions 0.9.0 or later, as Matthew noted in the comments, you need to use the renamed version, catplot.

sns.catplot(x='class', y='survival rate', hue='sex', data=df, kind='bar') 
like image 70
ayhan Avatar answered Oct 07 '22 10:10

ayhan