pythonmatplotlibseabornbar-chartcatplot

Changing width of bars created with catplot or barplot


I'm trying to create bar chart using seaborn.factorplot. My code looks like this:

import seaborn as sns
import matplotlib.pyplot as plt 
 
df = pd.read_csv('data.csv')
 
fg = sns.factorplot(x='vesselID', y='dur_min', hue='route', size=6, aspect=2, kind='bar', data=df)

my data.csv looks like this

,route,vesselID,dur_min
0,ANA-SJ,13,39.357894736842105
1,ANA-SJ,20,24.747663551401867
2,ANA-SJ,38,33.72142857142857
3,ANA-SJ,69,37.064516129032256
4,ED-KING,30,22.10062893081761
5,ED-KING,36,21.821428571428573
6,ED-KING,68,23.396551724137932
7,F-V-S,1,13.623239436619718
8,F-V-S,28,14.31294964028777
9,F-V-S,33,16.161616161616163
10,MUK-CL,18,13.953191489361702
11,MUK-CL,19,14.306513409961687
12,PD-TAL,65,12.477272727272727
13,PT-COU,52,27.48148148148148
14,PT-COU,66,28.24778761061947
15,SEA-BI,25,30.94267515923567
16,SEA-BI,32,31.0
17,SEA-BI,37,31.513513513513512
18,SEA-BR,2,55.8
19,SEA-BR,13,57.0
20,SEA-BR,15,54.05434782608695
21,SEA-BR,17,50.43859649122807

please click here to see the output

Now my question is how to change the width of the bar and I'm not able to achieve this by changing size and aspect.


Solution

  • In my case, I didn't have to define a custom function to change the width as suggested above (which btw didn't work for me as all the bars were unaligned). I simply added the attribute dodge=False to the argument of the seaborn plotting function and this made the trick! e.g.

    sns.countplot(x='x', hue='y', data=data, dodge=False);
    

    See additional reference here: https://github.com/mwaskom/seaborn/issues/871

    My bar plot looks now like this:

    enter image description here