python barplot宽度,如何在seaborn barplot上设置宽度

虽然在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()

yXgoQ.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值