3

My graph is ending up looking like this:

enter image description here

I took the original titanic dataset and sliced some columns and created a new dataframe via the following code.

Cabin_group = titanic[['Fare', 'Cabin', 'Survived']] #selecting certain columns from dataframe
Cabin_group.Cabin = Cabin_group.Cabin.str[0] #cleaning the Cabin column
Cabin_group = Cabin_group.groupby('Cabin', as_index =False).Survived.mean()
Cabin_group.drop([6,7], inplace = True) #drop Cabin G and T as instances are too low
Cabin_group['Status']= ('Poor', 'Rich', 'Rich', 'Medium', 'Medium', 'Poor') #giving each Cabin a status value.

So my new dataframe `Cabin_group' ends up looking like this:

  Cabin  Survived  Status
0     A  0.454545    Poor
1     B  0.676923    Rich
2     C  0.574468    Rich
3     D  0.652174  Medium
4     E  0.682927  Medium
5     F  0.523810    Poor

Here is how I tried to plot the dataframe

fig = plt.subplots(1,1, figsize = (10,4))
sns.barplot(x ='Cabin', y='Survived', hue ='Status', data = Cabin_group )
plt.show()

So a couple of things are off with this graph; First we have the bars A, D, E and F shifted away from their respective x-axis labels. Secondly, the bars itself seem to appear thinner/skinnier than my usual barplots.

Not sure how to shift the bars to their proper place, as well as how to control the width of the bars.

Thank you.

Romain
  • 19,910
  • 6
  • 56
  • 65
Moondra
  • 4,399
  • 9
  • 46
  • 104
  • Closed as a duplicate because the accepted [answer](https://stackoverflow.com/a/59080880/7758804) of the duplicate is older than the most upvoted answer here, and the answers are the same. – Trenton McKinney Aug 04 '23 at 15:00

2 Answers2

9

This can be achieved by doing dodge = False. It is handled in the new version of seaborn.

3

The bar are not aligned since it expects 3 bars for each x (1 for each distinct value of Status) and only one is provided. I think one of the solution is to map a color to the Status. As far as i know it is not possible to do thaht easily. However, here is an example of how to do that. I'm not sure about that since it seems complicated to simply map a color to a category (and the legend is not displayed).

# Creating a color mapping
Cabin_group['Color'] = Series(pd.factorize(Cabin_group['Status'])[0]).map(
                              lambda x: sns.color_palette()[x])

g = sns.barplot(x ='Cabin', y='Survived', data=Cabin_group, palette=Cabin_group['Color'])

enter image description here

When I see how simple it is in R ... But infortunately the ggplot implementation in Python does not allow to plot a geom_bar with stat = 'identity'.

library(tidyverse)

Cabin_group %>% ggplot() +
  geom_bar(aes(x = Cabin, y= Survived, fill = Status), 
           stat = 'identity')

enter image description here

Romain
  • 19,910
  • 6
  • 56
  • 65