gpt4 book ai didi

python - Matplotlib.patches.Wedge 动画不起作用

转载 作者:行者123 更新时间:2023-12-01 05:05:04 29 4
gpt4 key购买 nike

我刚刚开始为我的 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 来尝试代码,它确实有效(在代码中将该部分注释掉)。

最佳答案

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

# 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)可能能够判断这是否是一个错误或者是否有其他问题发生。

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

关于python - Matplotlib.patches.Wedge 动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25207432/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com