12

I would like to set the width of each bar on the barplot based on the number of times the column chrom has a particular value. I am setting width bars to be a list of occurrences:

list_counts =  plot_data.groupby('chrom')['gene'].count()

widthbars = list_counts.tolist()

Plotting the barplot as:

ax = sns.barplot(x = plot_data['chrom'], y = plot_data['dummy'], width=widthbars)

This gives me an error:

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

Is the width variable being set somewhere implicitly? How do I get the widths of each bar to be different?

cel
  • 30,017
  • 18
  • 97
  • 117
Preethi
  • 199
  • 1
  • 2
  • 7
  • FYI there is an issue on github (https://github.com/mwaskom/seaborn/issues/901) and a feature request to do it (not possible at the moment), so maybe in the future this will be less cumbersome. – My Work Dec 06 '22 at 15:03
  • From `seaborn v0.12.0`, use the `width` parameter, as shown in [How can I change the bar width in seaborn plots?](https://stackoverflow.com/q/76367661/7758804) – Trenton McKinney Jul 04 '23 at 20:55

1 Answers1

18

While there is no built-in way to do this in seaborn, you can manipulate the patches that sns.barplot creates on the matplotlib axes object.

Here is a minimal example for how to do it, based on the seaborn example for barplot here.

Note that each bar is allotted a space that is 1 unit wide, so it is important to normalise your counts to the interval 0-1.

import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.barplot(x="day", y="total_bill", data=tips)

# Set these based on your column counts
columncounts = [20,40,60,80]

# Maximum bar width is 1. Normalise counts to be in the interval 0-1. Need to supply a maximum possible count here as maxwidth
def normaliseCounts(widths,maxwidth):
    widths = np.array(widths)/float(maxwidth)
    return widths

widthbars = normaliseCounts(columncounts,100)

# Loop over the bars, and adjust the width (and position, to keep the bar centred)
for bar,newwidth in zip(ax.patches,widthbars):
    x = bar.get_x()
    width = bar.get_width()
    centre = x+width/2.

    bar.set_x(centre-newwidth/2.)
    bar.set_width(newwidth)

plt.show()

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • 2
    Nice. Because the question is about using a count measure for the widths, I'll mention that each bar is allotted a space that is 1 unit wide, so it will be good to add some sort of normalization to the width measurements otherwise the plot will be uninterpretable. – mwaskom Apr 25 '16 at 14:09
  • 1
    thanks for the suggestion. I think my edit covers that too – tmdavison Apr 25 '16 at 14:48