Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib.patches.Wedge动画不工作_Python_Animation_Matplotlib_Wedge - Fatal编程技术网

Python Matplotlib.patches.Wedge动画不工作

Python Matplotlib.patches.Wedge动画不工作,python,animation,matplotlib,wedge,Python,Animation,Matplotlib,Wedge,我刚刚开始制作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,

我刚刚开始制作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()

我用一个圆形补丁替换了补丁,并尝试了代码,它确实起了作用(在代码中注释掉了该部分)

代码似乎需要两个小改动:

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

  • 属性
    r
    定义半径,没有属性
    radius
    。另一方面,您可以(也可能应该)使用方法
    set\u radius()

  • 出于某种原因,需要手动调用内部方法
    \u recompute\u path
    ,以重新计算实际路径

  • 后者似乎是源代码中的遗漏,方法
    set\u radius
    可以调用
    \u recompute\u path
    。然而,有一些更聪明的人(至少@tcaswell)可以判断这是一个bug还是有其他问题


    (顺便问一下,你确定你想说
    patch_one.r+=i*0.0001
    ?这个动画看起来有点奇怪。也许你的意思是
    patch_one.r=1+i*.0001
    ?)

    谢谢你提供了清晰的测试代码!但是,复制和粘贴时要小心。代码现在是可插入的,没有修复一个小错误。(在第12行:
    patch_one=…
    )你说得对,楔块没有半径。我一直在研究θ1和θ2来创建一个旋转楔块,我用半径来代替它,以便使用与圆相同的代码。我尝试应用你的建议,但我没有set_radius()和_recompute_path方法。我使用的是
    matplotlib.\uuuuuu版本\uuuuuu='1.1.1rc'
    获取更新版本!(很抱歉,我没有看到任何其他合理的解决方案。)所以我所需要做的就是
    sudopip安装matplotlib[all]--升级
    谢谢@DrV