我的直方图的条形图没有显示,而且被切断了。

问题描述 投票:0回答:1

我写下了 这个 代码,程序完美运行,没有任何错误,但直方图的条形图在y轴上的1.0处被切断。为什么会出现这种情况?谢谢你的帮助

我的代码。

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
plt.hist(list1, bins=10, rwidth=0.9)
plt.ylabel("y")
plt.xlabel("x")
plt.show()

编辑:好的,我给它的bins值是10。但是如果我把bins的值改为比如说......

plt.hist(list1, bins=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

我得到的结果是

this.

现在怎么办?

注意:我想用plt.hist做一个直方图,不想用plt.bar做一个条形图。

python matplotlib histogram
1个回答
0
投票

直方图,也就是你正在绘制的东西,显示了你在测量中作为参数的分布情况。plt.hist(). 由于你的输入是不重复的数字,分布是均匀的,因此所有的高度都是一样的。你可能会有这样的印象:条形图被切掉了。但是调用 plt.gca().set(ylim = (0, 10)) 会显示所有的条形图都是一样的高度。直方图可能不是你要找的,而是一个条形图。如果是这样的话,请阅读以下内容。https:/matplotlib.org3.2.1api_as_genmatplotlib.pyplot.bar.html。


0
投票

解决方案:你应该使用 plt.bar 而不是 plt.hist

x = [xlabel for xlabel in range(len(list1))]
plt.bar(x, list1)

0
投票

不如你来吧 widthplt.hist:

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
plt.hist(list1, bins=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], width=0.9)
plt.ylabel("y")
plt.xlabel("x")
plt.show()

产出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.