虽然在seaborn中没有内置的方法来执行此操作,但您可以操作sns.barplot在matplotlib轴对象上创建的修补程序。
请注意,每个小柱被分配一个宽度为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()
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SUvdGW59CXt92YuIXdn1Waus2YhR3cuk2Lc9CX6MHc0RHaiojIsJye.png)