简体   繁体   English

如何消除 matplotlib 条形图中 x 轴的间隙?

[英]How to remove gaps in x axis in matplotlib bar chart?

I have this simple example:我有这个简单的例子:

from matplotlib import pyplot as plt
plt.bar(x=[0,1,2], height=[10,8,6], width=0.5)
plt.show()

How can I remove the 0.5, 1.5 between the bars so that the x axis has integers of 0, 1, 2 only?如何删除条形之间的 0.5、1.5,以便 x 轴只有 0、1、2 的整数? Thanks谢谢

在此处输入图像描述

Just add plt.xticks(range(3)) .只需添加plt.xticks(range(3))

from matplotlib import pyplot as plt
plt.bar(x=[0,1,2], height=[10,8,6], width=0.5)
plt.xticks(range(3))
plt.show()

在此处输入图像描述

Edit编辑

If you have gaps between your data, just use your x-values as input for xticks .如果您的数据之间存在差距,只需使用您的 x 值作为xticks的输入。

Code:代码:

from matplotlib import pyplot as plt

x_data = [0, 1, 2, 5, 7]
y_data = [10, 8, 6, 3, 12]
plt.bar(x=x_data, height=y_data, width=0.5)
plt.xticks(x_data)
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM