在 Python 中绘制动画箭袋 [英] Plotting animated quivers in Python

查看:27
本文介绍了在 Python 中绘制动画箭袋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Python 中为矢量(例如风)设置动画.我尝试在 pylab 中使用 quiver 函数,并与 matplotlib 中的 matplotlib.animation 结合使用.但是,结果显示 'QuiverKey' object is not subscriptable.我认为是因为我对这两个功能没有完全了解,或者只是这两个功能不匹配.下面是我的代码,实际上是matplotlib中quiver和animation函数的结合.

I am trying to animate a vector such as wind in Python. I tried to use quiver function in pylab and in combination with matplotlib.animation from matplotlib. However, the result says 'QuiverKey' object is not subscriptable. I think that it is because I don't understand fully about these two functions or just these two functions don't match together. Below is my code, it is actually the combination between quiver and animation functions from matplotlib.

def update_line(num, data, line):
    line.set_data(data[...,:num])
    return line,

X,Y = np.meshgrid(np.arange(0,2*np.pi,.2),np.arange(0,2*np.pi,.2) )  
U = np.cos(X)
V = np.sin(Y)

fig1 = plt.figure()
Q = quiver( X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
        pivot='mid', color='r', units='inches' )
data = quiverkey(Q, 0.5, 0.03, 1, r'$1 \frac{m}{s}$', fontproperties={'weight': 'bold'})
plt.axis([-1, 7, -1, 7])
title('scales with plot width, not view')
l, = plt.plot([], [], 'r-') 
plt.xlabel('x')
plt.ylabel('y')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=50, blit=True)
plt.show() 

推荐答案

这是一个让您入门的示例:

Here's an example to get you started:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

X, Y = np.mgrid[:2*np.pi:10j,:2*np.pi:5j]
U = np.cos(X)
V = np.sin(Y)

fig, ax = plt.subplots(1,1)
Q = ax.quiver(X, Y, U, V, pivot='mid', color='r', units='inches')

ax.set_xlim(-1, 7)
ax.set_ylim(-1, 7)

def update_quiver(num, Q, X, Y):
    """updates the horizontal and vertical vector components by a
    fixed increment on each frame
    """

    U = np.cos(X + num*0.1)
    V = np.sin(Y + num*0.1)

    Q.set_UVC(U,V)

    return Q,

# you need to set blit=False, or the first set of arrows never gets
# cleared on subsequent frames
anim = animation.FuncAnimation(fig, update_quiver, fargs=(Q, X, Y),
                               interval=50, blit=False)
fig.tight_layout()
plt.show()

这篇关于在 Python 中绘制动画箭袋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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