小编典典

在Python中绘制动画抖动

python

我正在尝试设置一个向量的动画,比如Python中的wind。我试着用箭袋
在pylab中的功能以及与matplotlib.animation文件从
matplotlib。但是,结果显示“QuiverKey”对象不是
可下标’。我想那是因为我不完全了解
这两个函数或者只是这两个函数不匹配。下面是
我的代码,它实际上是箭袋和动画功能的结合
从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()

阅读 214

收藏
2020-12-20

共1个答案

小编典典

下面是一个让您开始学习的示例:

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()
2020-12-20