首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matplotlib.patches.Wedge动画不工作

Matplotlib.patches.Wedge动画不工作
EN

Stack Overflow用户
提问于 2014-08-08 15:53:31
回答 1查看 1.1K关注 0票数 1

我刚刚开始动画我的matplotlib补丁。在我目前的工作中,我想显示一个可视化,在那里有一个楔形,可以随着时间的推移改变它的大小。然而,我得到了一个静态的楔子,原因超出了我的视野。

代码语言:javascript
复制
#!/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,它确实起了作用(在代码中注释掉了这个部分)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-08 17:56:43

代码似乎需要两个小的修改:

代码语言:javascript
复制
# animation function.  This is called sequentially
def animate(i):
    patch_one.r +=i*0.0001
    patch_one._recompute_path()
    return patch_one,

为什么:

  1. 属性r定义半径,没有属性radius。另一方面,您可以(而且可能应该)使用set_radius()方法。
  2. 出于某种原因,需要手动调用内部方法_recompute_path来重新计算实际路径。

后者似乎是源中的遗漏,set_radius可以调用_recompute_path的方法。然而,也有一些更聪明的人(至少@tcaswell)能够分辨这是一个bug还是其他的事情。

(顺便问一下,你确定要说patch_one.r += i*0.0001吗?这个动画看上去有点奇怪。也许你是说patch_one.r = 1 + i*.0001?)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25207432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档