Python matplotlib箭头给出ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()

Python matplotlib箭头给出ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all(),python,matplotlib,Python,Matplotlib,尝试使用matplotlib的arrow函数绘制箭头时出错 >>> import matplotlib.pyplot as plt >>> import numpy as np >>> arrow_start = np.random.normal(0, 1, [100, 2]) >>> arrow_vector = np.random.normal(0, 0.05, [100, 2]) >>> plt.ar

尝试使用matplotlib的
arrow
函数绘制箭头时出错

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> arrow_start = np.random.normal(0, 1, [100, 2])
>>> arrow_vector = np.random.normal(0, 0.05, [100, 2])
>>> plt.arrow(x=arrow_start[:, 0], y=arrow_start[:, 1], 
...           dx=arrow_vector[:, 0], dy=arrow_vector[:, 1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\programdata\miniconda3\lib\site-packages\matplotlib-2.2.2-py3.6-win-amd64.egg\matplotlib\axes\_axes.py", line 4844, in arrow
    a = mpatches.FancyArrow(x, y, dx, dy, **kwargs)
  File "c:\programdata\miniconda3\lib\site-packages\matplotlib-2.2.2-py3.6-win-amd64.egg\matplotlib\patches.py", line 1255, in __init__
    if not length:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>将matplotlib.pyplot作为plt导入
>>>将numpy作为np导入
>>>arrow\u start=np.random.normal(0,1[100,2])
>>>arrow_vector=np.随机.正态(0,0.05,[100,2])
>>>plt.arrow(x=箭头开始[:,0],y=箭头开始[:,1],
…dx=箭头向量[:,0],dy=箭头向量[:,1])
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“c:\programdata\miniconda3\lib\site packages\matplotlib-2.2-py3.6-win-amd64.egg\matplotlib\axes\\u axes.py”,第4844行,箭头所示
a=mpatches.FancyArrow(x,y,dx,dy,**kwargs)
文件“c:\programdata\miniconda3\lib\site packages\matplotlib-2.2-py3.6-win-amd64.egg\matplotlib\patches.py”,第1255行,在u init中__
如果不是长度:
ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()

此错误消息与实际发生的错误关系不大。我在下面给出了自己的解决方案,以便其他人可以从我的调试中学习。

错误消息没有解释这里发生了什么:
matplotlib.pyplot.arrow
和关联的
matplotlib.axes.axes.arrow
matplotlib.patches.FancyArrow
不支持一次绘制多个箭头

这个问题很容易通过以下方式解决

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> arrow_start = np.random.normal(0, 1, [100, 2])
>>> arrow_vector = np.random.normal(0, 0.05, [100, 2])
>>> for i in range(len(arrow_start.shape[0])):
...     plt.arrow(x=arrow_start[i, 0], y=arrow_start[i, 1], 
...               dx=arrow_vector[i, 0], dy=arrow_vector[i, 1])