Pandas Plot,如何控制条宽和间隙

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34233347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:22:17  来源:igfitidea点击:

Pandas Plot, how to control the bar width and the gaps

pythonpandasplotbar-chart

提问by xpt

Hope this is an easy question -- how to control the bar width and the gaps when plotting with Pandas?

希望这是一个简单的问题——在使用 Pandas 绘图时如何控制条宽和间隙?

Full code is here, reposted below:

完整代码在这里,转发如下:

df = pd.DataFrame({
        'person':[x*3 for x in list('ABCDEF')],
        'score1':np.random.randn(6),
        'score2':np.random.randn(6),
        'score3':np.random.randn(6),
        'score4':np.random.randn(6),
        'score5':np.random.randn(6)
                   })
print(df)
ax = df.set_index(['person']).plot(kind='barh')
ax.invert_yaxis()

enter image description here

在此处输入图片说明

The result bar width is too thin and the gaps is too wide, how can I fix it? Thanks.

结果条宽度太细,间隙太宽,我该如何解决?谢谢。

回答by Mike Müller

You can set the widthof the bars:

您可以设置width条形:

ax = df.set_index(['person']).plot(kind='barh', width=1.0)

The result looks like this:

结果如下所示:

enter image description here

在此处输入图片说明

Make them thinner again:

让它们再次变薄:

ax = df.set_index(['person']).plot(kind='barh', width=0.5)

enter image description here

在此处输入图片说明