只有一个数据时如何设置matplotlib条形图宽度 [英] how to set matplotlib bar graph width when there is only one data

查看:70
本文介绍了只有一个数据时如何设置matplotlib条形图宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 matplotlib 生成图表.生成条形图时出现一个问题.如果只有一个数据,如下图所示,则即使设置了宽度,图形的宽度也会覆盖整个区域.示例:

I am using matplotlib to generate a graph. There is one problem while generating bar graph. If there is only one data like shown below,the width of the graph is covering the whole area,evenif the width is set. Example :

     x = [3] 
     y = [10] 
     bar(x, y, width=0.2,align='center')

推荐答案

width 控制以数据"为单位的条形宽度.默认情况下,图形的限制会自动缩放到您的数据限制,在这种情况下,该限制就是单个条形的宽度.您只需要轴限制.

The width controls the width of the bar in 'data' units. By default the limits of the graph are auto-scaled to your data limits, in this case that is the width of your single bar. You just need the axis limits.

x = [3]
y = [10]
fig, ax = plt.subplots(1, 1)
ax.bar(x, y, width=0.2)
ax.set_xlim(0, 5)
plt.show()

这篇关于只有一个数据时如何设置matplotlib条形图宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆