[英]reduce space between bars without changing width
您可以更改xlim
s。 使用分類索引,x 位置在內部編號為0, 1, ...
Matplotlib 在每邊添加了一個默認的 5% 的填充,當只有兩個條時,這太短了。 或者,您還可以更改ylim
欄頂部的文本騰出更多空間:
from matplotlib import pyplot as plt
gender = ['M', 'F']
numbers = [1644, 1771]
bars = plt.bar(gender, numbers, width=0.1, bottom=None, align='center', data=None)
for x, y in zip(gender, numbers):
plt.text(x, y, f'{y}\n', ha='center', va='center')
plt.xlim(-0.9, len(gender) - 1 + 0.9)
plt.ylim(0, max(numbers) * 1.1)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.