Python中的箭头动画 [英] Arrow animation in Python

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

问题描述

首先,我刚刚开始学习Python。在过去的几个小时中,我一直在努力尝试更新箭头属性,以便在剧情动画期间更改它们。

First of all, I am just starting to learn Python. I have been struggling during the last hours trying to update the arrow properties in order to change them during a plot animation.

在彻底寻找答案之后,我检查了一下可以通过修改属性中心(例如 circle.center = new_coordinates )来更改圆形补丁中心。但是,我找不到将这种机制外推到箭头补丁的方法...

After thoroughly looking for an answer, I have checked that it is possible to change a circle patch center by modifying the attribute 'center' such as circle.center = new_coordinates. However, I don't find the way to extrapolate this mechanism to an arrow patch...

到目前为止,代码是:

import numpy as np, math, matplotlib.patches as patches
from matplotlib import pyplot as plt
from matplotlib import animation

# Create figure
fig = plt.figure()    
ax = fig.gca()

# Axes labels and title are established
ax = fig.gca()
ax.set_xlabel('x')
ax.set_ylabel('y')

ax.set_ylim(-2,2)
ax.set_xlim(-2,2)
plt.gca().set_aspect('equal', adjustable='box')

x = np.linspace(-1,1,20) 
y  = np.linspace(-1,1,20) 
dx = np.zeros(len(x))
dy = np.zeros(len(y))

for i in range(len(x)):
    dx[i] = math.sin(x[i])
    dy[i] = math.cos(y[i])
patch = patches.Arrow(x[0], y[0], dx[0], dy[0] )


def init():
    ax.add_patch(patch)
    return patch,

def animate(t):
    patch.update(x[t], y[t], dx[t], dy[t])   # ERROR
    return patch,

anim = animation.FuncAnimation(fig, animate, 
                               init_func=init, 
                               interval=20,
                               blit=False)

plt.show()

尝试了几个选项之后,我认为功能更新可以使我更接近解决方案。但是,我得到了错误:

After trying several options, I thought that the function update could somehow take me closer to the solution. However, I get the error:

TypeError: update() takes 2 positional arguments but 5 were given

如果我仅通过定义动画功能(如下所示)在每个步骤中添加一个补丁,我得到的结果将显示在附件中。

If I just add one more patch per step by defining the animate function as shown below, I get the result shown in the image attached.

def animate(t):
    patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
    ax.add_patch(patch)
    return patch,

错误的动画

我试图添加一个patch.delete语句并创建一个新的补丁程序作为更新机制,但这会导致出现空动画...

I have tried to add a patch.delete statement and create a new patch as update mechanism but that results in an empty animation...

推荐答案

ax.add_patch(patch)之前添加 ax.clear()

def animate(t):

    ax.clear() 

    patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
    ax.add_patch(patch)

    return patch,






编辑::删除一个补丁


  • 使用 ax.patches.pop(index)

在您的示例中,只有一个补丁,因此您可以使用 index = 0

In your example is only one patch so you can use index=0

def animate(t):

    ax.patches.pop(0) 

    patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
    ax.add_patch(patch)

    return patch,


  • 使用 ax.patches.remove(object)

    需要 global 来获取/设置带有<$的外部 patch c $ c>箭头

    It needs global to get/set external patch with Arrow

    def animate(t):
    
        global patch
    
        ax.patches.remove(patch) 
    
        patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
        ax.add_patch(patch)
    
        return patch,
    


  • BTW::获取可用于的属性列表update()

    print( patch.properties().keys() )
    
    dict_keys(['aa', 'clip_path', 'patch_transform', 'edgecolor', 'path', 'verts', 'rasterized', 'linestyle', 'transform', 'picker', 'capstyle', 'children', 'antialiased', 'sketch_params', 'contains', 'snap', 'extents', 'figure', 'gid', 'zorder', 'transformed_clip_path_and_affine', 'clip_on', 'data_transform', 'alpha', 'hatch', 'axes', 'lw', 'path_effects', 'visible', 'label', 'ls', 'linewidth', 'agg_filter', 'ec', 'facecolor', 'fc', 'window_extent', 'animated', 'url', 'clip_box', 'joinstyle', 'fill'])
    

    因此您可以使用 update 更改颜色-`facecolor

    so you can use update to change color - `facecolor

    def animate(t):
        global patch
    
        t %= 20 # get only 0-19 to loop animation and get color t/20 as 0.0-1.0
    
        ax.patches.remove(patch)
    
        patch = patches.Arrow(x[t], y[t], dx[t], dy[t])
    
        patch.update({'facecolor': (t/20,t/20,t/20,1.0)})
    
        ax.add_patch(patch)
    
        return patch,
    

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

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