You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Player类动画matplotlib quiver图时触发ValueError问题

问题:Matplotlib中使用Player类动画Quiver向量报错

我用自定义的Player类(自带播放控制台)实现动画,已经成功完成热力图等效果,但在动画quiver生成的向量时遇到问题。

我的目标是动画一系列箭头,t时刻箭头的幅值等于信号2*sin(t)的值,示例代码如下:

fig,ax=plt.subplots(1)
t = np.linspace(0,6*np.pi, num=100)
s = 2*np.sin(t)
radius=s
x= np.multiply(radius,np.cos(t))
y =np.multiply( radius, np.sin(t))
point_vector,=ax.quiver(0,0,[],[])

def update(i):
    point_vector.set_UVC(x[i],y[i])

ani = Player(fig, update, maxi=len(t)-1)
plt.show()

运行后报错:

ValueError: Argument U has a size 0 which does not match 1, the number of arrow positions

我认为错误源于quiver命令本身。


解决方案

错误的核心原因是:初始化quiver时传入了空的U和V参数([]),导致quiver对象内部的箭头位置数量(1个,对应(0,0))与向量维度(0个)不匹配。

解决方法是初始化时传入非空的初始向量,后续再通过set_UVC更新:

fig,ax=plt.subplots(1)
t = np.linspace(0,6*np.pi, num=100)
s = 2*np.sin(t)
radius=s
x= np.multiply(radius,np.cos(t))
y =np.multiply( radius, np.sin(t))
# 初始化时传入第一个时刻的向量,替代空列表
point_vector,=ax.quiver(0,0,x[0],y[0])

def update(i):
    point_vector.set_UVC(x[i],y[i])
    # 可选:固定坐标轴范围,避免箭头超出画布
    ax.set_xlim(-2.5, 2.5)
    ax.set_ylim(-2.5, 2.5)

ani = Player(fig, update, maxi=len(t)-1)
plt.show()

关键修改说明:

  • 初始化quiver时使用x[0]和y[0],确保初始向量的数量与箭头位置数量(1)一致
  • 添加坐标轴范围设置可以保证箭头在动画过程中始终在视野内

内容的提问来源于stack exchange,提问作者fishbacp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 17:05:51