gpt4 book ai didi

python matplot.hist - 删除条形图之间的间隙

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:31 25 4
gpt4 key购买 nike

我希望创建一个条形之间没有间隙的直方图。尝试添加参数 binrange 但没有成功。

import numpy as np
from matplotlib import pyplot as plt

population = 100
possible_answers = 4
scores = np.random.choice(range(possible_answers),population)
plt.hist(scores)
plt.show()

histogram with gaps between the bars

最佳答案

您需要使用bins参数来调整使用哪些容器。

import numpy as np
from matplotlib import pyplot as plt

population = 100
possible_answers = 4
scores = np.random.choice(range(possible_answers),population)
plt.hist(scores, bins=range(possible_answers+1), ec="k")

plt.show()

enter image description here

或者,如果您想移动垃圾箱

plt.hist(scores, bins=np.arange(possible_answers+1)-0.5, ec="k")

enter image description here

关于python matplot.hist - 删除条形图之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48853667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com