2015-10-16 42 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') 
相關問題