How to set the width on a sea barrel

I would like to set the width of each bar on the dash bar depending on the number of times the column chromhas a specific value. I set the width strips as a list of occurrences:

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

widthbars = list_counts.tolist()

Barglot Bookmark 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 a variable width value somewhere implicit? How to increase the width of each column?

+4
source share
1 answer

While there is no built-in way to do this in the sea, you can manipulate the patches that the sns.barplotmatplotlib axes create on the object.

, , .

, 1 , 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

+4

All Articles