Python AssertionError' height'必须是长度为0或标量Bar(arange)函数

时间:2015-10-16 02:44:54

标签: python python-2.7

问题出在这里?使用2.7。感谢。

这是错误:

AssertionError:不兼容的尺寸:参数'高度'必须是0或标量长度

from numpy import zeros, random

m=zeros(10,int)
for i in range(10000):
    n=random.random()
    if 0.0<=n and n<0.1: m[0]=m[0]+1
    if 0.1<=n and n<0.2: m[1]=m[1]+1
    if 0.2<=n and n<0.3: m[2]=m[2]+1
    if 0.3<=n and n<0.4: m[3]=m[3]+1
    if 0.4<=n and n<0.5: m[4]=m[4]+1
    if 0.5<=n and n<0.6: m[5]=m[5]+1
    if 0.6<=n and n<0.7: m[6]=m[6]+1
    if 0.7<=n and n<0.8: m[7]=m[7]+1
    if 0.8<=n and n<0.9: m[8]=m[8]+1
    if 0.9<=n and n<1.0: m[9]=m[9]+1
print m

from pylab import *
bar(arange(0.1,0.1),m,width=0.1)
#show()
savefig('5.4graph.png')

2 个答案:

答案 0 :(得分:0)

这应该可以达到你想要的效果,尽管它可能对你没有任何意义:

# this does pretty much what you're trying to do with your for loop
m = map(lambda x: (random.random()/10)+1+.1*x,range(10))
print m
bar(arange(10)+.1,m)
show()
#or savefig('test.png')

enter image description here

答案 1 :(得分:0)

import numpy as np
import matplotlib.pyplot as plt



x = np.random.random(1000000)

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)

n, bins, patches = ax1.hist(x,25,normed=True)
ax1.set_title('Distribution from random numbers')

#plt.show()


plt.savefig('histogram1.png')