python matplotlib条形宽度不可控

标签 python matplotlib bar-chart

我有一个使用 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()

每次只绘制一根柱时,它都会填满图表的一半,如下所示:

enter image description here

如何解决这个问题?我尝试改变 bar_width 但没有成功。

最佳答案

它不起作用,因为你只给它一个数据点,所以宽度实际上并不重要,因为它显示整个条形加上它周围的一些小空间,无论你将宽度设置为多少。作为一个最小的例子,我省略了杂项。

比较以下两个示例,看看 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()

enter image description here

对比

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

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

enter image description here

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

相关文章:

Python FastAPI "Post Unprocessable Entity"错误

python - 根据不等式方程填充图像的区域

python - 使用 pandas 或 seaborn 通过条形图制作回归线

r - 意味着具有置信区间的条形图?

javascript - 是否可以在 Google 条形图上放置趋势线?

python - 如何将第一个查询的值传递到下一个要使用的查询

Python 更快地打印 sympy 矩阵

python - MySQL:自动化此查询链(来自值的新列)

python - 如何绘制列中的某些值(子图)

r - 为 geom_bar 分配连续的填充颜色