【问题标题】:How can I approximate the bars of a chart without changing the width of those bars? (Matplotlib)如何在不更改这些条形宽度的情况下近似图表的条形? (Matplotlib)
【发布时间】:2019-09-14 21:46:27
【问题描述】:

所以,基本上我想要的是减少两个条之间的距离,但我在互联网上阅读的所有解决方案都是人们改变条的宽度,这不是我想要的。

这是我的图表:

x = ['Goal (95%)','Achieved (100%)']
y = [95,100]

plt.bar(x,y,color = ('darkcyan','green'), width = 0.2)
plt.ylabel('Percentage (%)')

我希望是这样的:

如果我改变宽度,我可以减少距离,但是条变得很厚,我希望它们变薄。

plt.bar(x,y,color = ('darkcyan','green'), width = 0.8)
plt.ylabel('Percentage (%)')

【问题讨论】:

    标签: python matplotlib width bar-chart data-science


    【解决方案1】:

    通过更改图形大小然后更新宽度,我得到了非常接近您想要工作的东西。

    plt.figure(figsize=(3,4))
    plt.bar(x,y,color = ('darkcyan','green'), width = 0.8)
    

    通常,当您缩小图形尺寸时,条形会变得更窄,因此将其与较粗的条形结合似乎可以解决问题。您可以稍微调整一下数字,以获得您想要的结果。

    【讨论】:

    • 您可以在末尾添加plt.tight_layout(),以确保Y标签没有被裁剪。