1

我刚刚开始为我的 matplotlib 补丁制作动画。在我目前的工作中,我想显示一个可视化,其中存在一个随时间改变其大小的楔形。然而,我得到了一个静态楔子,原因超出了我的想象。

#!/usr/bin/env python
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import matplotlib.patches

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(-2, 2), ylim=(-2, 2))
plt.grid(True)
# patch_one = matplotlib.patches.Circle((0, 0), 1)
patch_one = matplotlib.patches.Wedge((0,0), 1, -10, 10)

# initialization function: plot the background of each frame
def init():
    patch_one.radius = 1
    ax.add_patch(patch_one)
    return patch_one,

# animation function.  This is called sequentially
def animate(i):
    patch_one.radius +=i*0.0001
    return patch_one,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

plt.show()

我通过用循环补丁替换 patch_one 来尝试代码,它确实有效(在代码中留下了注释掉的那部分)。

4

1 回答 1

3

The code seems to need two small changes:

# animation function.  This is called sequentially
def animate(i):
    patch_one.r +=i*0.0001
    patch_one._recompute_path()
    return patch_one,

Why:

  1. the attribute r defines the radius, there is no attribute radius. On the other hand, you may (and probably should) use the method set_radius().

  2. For some reason or another, the internal method _recompute_path needs to be called manually to recompute the actual path.

The latter one seems like an omission in the source, the method set_radius could call _recompute_path. However, there are wiser people (at least @tcaswell) who may be able to tell if this is a bug or if there is something else going on.

(By the way, are you sure you want to say patch_one.r += i*0.0001? The animation looks a bit odd with that. Maybe you mean patch_one.r = 1 + i*.0001?)

于 2014-08-08T17:56:43.803 回答