2015-10-16 95 views
0

这是什么问题?使用2.7。谢谢。Python AssertionError'height'必须是长度为0或标量Bar(arange)函数

这是错误:

Asse田:不兼容大小:参数“高度”必须是一个长度为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') 
+0

我觉得你在'bar'的调用中在'arange'调用中有一个错字。你正在调用'arange(0.1,0.1)',这将产生一个空数组。我想你想输入'arange(0,1,0.1)'。 – Blckknght

回答

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

嗨,我实际上是这样做的,请看下面 – Oscar

+0

不错,很高兴你明白了:) – rofls

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') 
相关问题