我有一个使用 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()
每次只绘制一根柱时,它都会填满图表的一半,如下所示:
如何解决这个问题?我尝试改变 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()
对比
import matplotlib.pyplot as plt
index = [0 ]
counts = [7 ]
bar_width=0.1
plt.bar(index, counts, bar_width )
plt.show()
关于python matplotlib条形宽度不可控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36343439/