Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复直方图_Python_Python 3.x_Matplotlib_Histogram - Fatal编程技术网

Python 如何修复直方图

Python 如何修复直方图,python,python-3.x,matplotlib,histogram,Python,Python 3.x,Matplotlib,Histogram,我想用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() 我怎样才能把这些条放在整数的地方呢?为什么我

我想用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()


我怎样才能把这些条放在整数的地方呢?为什么我的图表也显示浮点数呢?

您制作了
条形图,但不使用它。如果将
hist
bin
选项设置为
bar
,则一切正常

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

要将
yticks
设置为仅整数值,可以使用模块中的:


也许这个问题的答案已经对你有所帮助:hist默认使用10个箱子,但在最小到最大的范围内,不是0到10,因为你的数字是随机的。我想他想从Y轴上删除0.5、1.5等。谢谢,如何添加密度图?像这样的:我是说红色曲线
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()