Python断言错误';高度';长度必须为0或标量条(arange)函数
这里有什么问题?使用2.7。谢谢 这就是错误: AssertionError:大小不兼容:参数“高度”的长度必须为0或标量Python断言错误';高度';长度必须为0或标量条(arange)函数,python,python-2.7,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
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')
从numpy导入零,随机
m=零(10,int)
对于范围(10000)内的i:
n=random.random()
如果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')
我想您的arange
调用中的bar
中有一个输入错误。您正在调用arange(0.1,0.1)
,这将生成一个空数组。我想你想输入arange(0,1,0.1)
。嗨,实际上我自己就是这样做的,见下文,很高兴你弄明白了:)
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')