我正在尝试使用以下方式生成和绘制随机数:
from numpy import random
import matplotlib.pyplot as plt
z = 15 + 2*random.randn(200) #200 elements, normal dist with mean = 15, sd = 2
plt.plot(z)
plt.show(z)
绘制图表,但Python(2.7.5)冻结,我收到错误
Traceback (most recent call last):
File "G:\Stage 2 expt\e298\q1.py", line 25, in <module>
plt.show(z)
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 145, in show
_show(*args, **kw)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 90, in __call__
if block:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
当我像这样执行for循环时,它完全没问题:
from numpy import random
from pylab import plot,show
yvec = [] # set up an empty vector
for i in range(200): # want 200 numbers
yy = 25 + 3*random.randn() # normal dist with mean = 15, sd = 2
yvec.append(yy) # enter yy into vector
plot(yvec)
show(yvec)
请有人澄清一下吗?
答案 0 :(得分:4)
函数pylab.show
不接受列表或数组,它采用可选的布尔值(当然不是数据数组)。第一个示例中的numpy数组不能隐式转换为布尔值,从而引发错误。然而,第二个可以转换为布尔值,如果非空,它将评估为True
。
要修复它,只需在没有任何参数的情况下调用show
。