Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to change barplot width in python using seaborn

I have the following code trying make a bar plot in python using seaborn, based simply on a two-column dataframe, with a "Category" column, and "Value" column with float values:

dataplot = sns.barplot(x = 'Category',
            y = 'Value',
            data = df, hue='Category')

This produces a bar plot plot with very skinny bars, going into both positive and negative values. I want to make the bars thicker/wider, and so I try to use the width parameter from seaborn, and I try:

dataplot = sns.barplot(x = 'Category',
            y = 'Value',
            data = df, hue='Category', width=2)

I am just testing out 2 as a value here for width, but I get this error that I do not understand:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: bar() got multiple values for argument 'width'

I do not understand what these multiple values is referring to. How can I correctly enter the width parameter into my bar plot code so that I can simply make my bars thicker?

>Solution :

Okay, so I used a test dataset from seaborn and was able to recreate the same error as you…this is honestly really confusing to me as I also could not figure out the problem. But, in the meantime, I found a workaround to the problem that can be used.

width = 0.5
penguins= sns.load_dataset("penguins")
dataplot = sns.barplot(data=penguins, y="body_mass_g", x='island')
for bar in dataplot.patches:
    x = bar.get_x()
    width = bar.get_width()
    centre = x + width / 2.
    bar.set_x(centre - width / 2.)
    bar.set_width(width)  # set new width to 0.5

You should be able to use this with your code, although again, I’m confused why you ran into this error in the first place.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading