0

I simply want to manually adjust the width of a barplot in seaborn but I run into the error TypeError: bar() got multiple values for argument 'width'

This is what produced the error:

#create DataFrame
import pandas as pd
import seaborn as sns

df = pd.DataFrame({'employee': ['Andy'],
                   'sales': [22]})

#create bar plot with width = 0.4
sns.barplot(x='employee', y='sales', data=df, width=0.4)

As a workaround, I tried setting patch.set_width():

#create DataFrame
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({'employee': ['Andy'],
                   'sales': [22]})

#create bar plot with width = 0.4 
# now using patch.set_width
fig, ax = plt.subplots(figsize=(5,5))
ax.patch.set_width(0.6)
ax = sns.barplot(x='employee', y='sales', data=df)

However, this decenters the bar, which I don't want. Thanks for your help!

pleo
  • 11
  • 3
  • 3
    I don't have issues with pandas 1.5.3, matplotlib 3.6.3, seaborn 0.12.2, you might want to update – mozway Mar 09 '23 at 10:16
  • Same for me for same versions. (except for matplotlib 3.5.2). I can reproduce the problem with `seaborn==0.11.2` – Corralien Mar 09 '23 at 10:17
  • If you are on older version and using second option, just before the `ax=barplot()`, you can move the bar to the right a little by adding `ax.patch.set_x(ax.patch.get_x()+0.2)` – Redox Mar 09 '23 at 10:19
  • 1
    Seaborn 12.2 has a width parameter, 11.2 does not hav a width parameter. – Trenton McKinney Mar 09 '23 at 11:13
  • See https://github.com/mwaskom/seaborn/issues/3137 – JohanC Mar 09 '23 at 11:17

0 Answers0