使用 matplotlib 动画 3D 矢量 [英] Animating a 3D vector with matplotlib

查看:45
本文介绍了使用 matplotlib 动画 3D 矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用matplotlib为3D图形中的矢量箭头的大小和位置设置动画.据我所知,我无法使用Axes3D.quiver设置矢量的位置动画,并且3D图形没有matplotlib.pyplot.arrow.有没有办法做到这一点,或者我需要看看其他选择吗?

I need to animate both the magnitude and the position of a vector arrow in a 3D figure with matplotlib. As far as I can tell, I cannot animate the position of the vectors with Axes3D.quiver and there is no matplotlib.pyplot.arrow for a 3D figure. Is there a way to do this or do I need to look at other options?

推荐答案

箭袋没有更新功能,但是您可以轻松删除旧的箭袋并在每帧中绘制一个新的箭袋./p>

quiver does not have an updating function, but you can easily remove an old quiver and plot a new one in each frame.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots(subplot_kw=dict(projection="3d"))

def get_arrow(theta):
    x = np.cos(theta)
    y = np.sin(theta)
    z = 0
    u = np.sin(2*theta)
    v = np.sin(3*theta)
    w = np.cos(3*theta)
    return x,y,z,u,v,w

quiver = ax.quiver(*get_arrow(0))

ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.set_zlim(-2, 2)

def update(theta):
    global quiver
    quiver.remove()
    quiver = ax.quiver(*get_arrow(theta))

ani = FuncAnimation(fig, update, frames=np.linspace(0,2*np.pi,200), interval=50)
plt.show()

这篇关于使用 matplotlib 动画 3D 矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆