我有一个使用python matplotlib绘制的条形图:

import matplotlib.pyplot as plt
unique=[200]
index = [0]
counts = [7]
alpha=0.4
bar_width=0.1
color='b'
plt.bar(index, counts, bar_width, alpha=alpha, color=color)
# set x and y labels
plt.xlabel('Status')
plt.ylabel('Counts')
# set title and plot
plt.title('Status Counts')
plt.xticks(index + bar_width/2, unique)
plt.show()


每次只绘制一个条形图时,它将填满图表的一半,如下所示:

python - python matplotlib条宽度不可控制-LMLPHP

如何解决?我试图改变bar_width没有运气。

最佳答案

这是行不通的,因为您只给它一个数据点,所以宽度的确无关紧要,因为它显示了整个条形图以及周围的一些小空间,无论您将宽度设置为什么。举个最小的例子,我省去了杂费。

比较以下两个示例,可以看到条形宽度确实起作用:

import matplotlib.pyplot as plt
index = [0,1,2,3,4,5,6]
counts = [7,7,19,2,3,0,1]

bar_width=0.1
plt.bar(index, counts, bar_width )
plt.show()


python - python matplotlib条宽度不可控制-LMLPHP



import matplotlib.pyplot as plt
index = [0 ]
counts = [7 ]

bar_width=0.1
plt.bar(index, counts, bar_width )
plt.show()


python - python matplotlib条宽度不可控制-LMLPHP

关于python - python matplotlib条宽度不可控制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36343439/

10-14 19:21
查看更多