gpt4 book ai didi

python - Matplotlib 动画不接受圆形补丁作为 fargs

转载 作者:行者123 更新时间:2023-12-01 08:30:28 24 4
gpt4 key购买 nike

import math
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import Circle
from matplotlib import animation
from utils import rgb2hex
from tg import small_tg

matplotlib.rcParams["figure.figsize"]=(10, 5)
matplotlib.rcParams['toolbar'] = 'None'

fig, ax = plt.subplots()
ax.axis("equal")
ax.set_xlim(-10, 10)
ax.set_ylim(-5, 5)
ax.axis("off")

state_nodes = []
for state in small_tg["states"]:
center = small_tg["states"][state]["graphic_properties"]["position"]
state_nodes.append(Circle(center, 0.2, color = rgb2hex(255, 255, 255)))

def animate(i, state):
print("&", i ,state)
y = math.ceil((abs(i-100))*2.55)
print(i, y, (abs(i-100))*2.55)
state.set_color = rgb2hex(y,y,y)
ax.add_artist(state)
return state,

for state in state_nodes:
print(state)
animation.FuncAnimation(fig, animate, fargs = (state,), frames=201, interval=1, blit=True, repeat = False)

plt.show()

for 循环内的 print 语句正在显示状态,但是 animate 函数内的 print 语句没有显示任何内容,这意味着该函数未被调用。我找不到任何逻辑错误。请帮忙。

最佳答案

如果您想同时对多个元素进行动画处理,则不应运行多个 FuncAnimation。相反,运行一个动画,并在单个 animate() 函数中为所有艺术家设置动画。

我无法运行您的代码,但我尝试在本演示中尽量接近我能理解的代码:

import math
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib import animation
from matplotlib.colors import rgb2hex

fig, ax = plt.subplots()
ax.axis("equal")
ax.set_xlim(-10, 10)
ax.set_ylim(-5, 5)
ax.axis("off")

state_nodes = [Circle(center, 0.2, color='k') for center in np.random.normal(loc=0, scale=1, size=(5,2))]
for state in state_nodes:
ax.add_artist(state)

def animate(i, states):
c = (i/255)
for state in states:
dx,dy = np.random.normal(loc=0, scale=0.1, size=(2,))
x,y = state.get_center()
state.set_center((x+dx,y+dy))
state.set_color(rgb2hex((c,c,c)))
return states


ani = animation.FuncAnimation(fig, animate, fargs = (state_nodes,), frames=201, interval=25, blit=True, repeat = False)
plt.show()

enter image description here

关于python - Matplotlib 动画不接受圆形补丁作为 fargs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53924740/

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