我怎样才能修复我的直方图

how can I fix my histogram plot

我想用 python 绘制一个非常简单的直方图。这是我的代码:

from numpy import *
from matplotlib.pyplot import*
from random import*
nums = []
N = 10
for i in range(N):
    a = randint(0,10)
    nums.append(a)

bars= [0,1,2,3,4,5,6,7,8,9]
hist(nums)

show()

This is the result

如何将条形图放在整数位置?为什么我的图表也显示浮点数?

你制作了 bars 但后来却没有使用它。如果将histbins选项设置为bars,一切正常

bars= [0,1,2,3,4,5,6,7,8,9]
hist(nums,bins=bars)

要将 yticks 设置为仅整数值,您可以使用 MultipleLocator from the matplotlib.ticker 模块:

from numpy import *
from matplotlib.pyplot import*
import matplotlib.ticker as ticker
from random import*

nums = []
N = 10
for i in range(N):
    a = randint(0,10)
    nums.append(a)

bars= [0,1,2,3,4,5,6,7,8,9]
hist(nums,bins=bars)

gca().yaxis.set_major_locator(ticker.MultipleLocator(1))

show()