简体   繁体   English

Matplotlib 动画不接受圆形补丁作为farg

[英]Matplotlib animation not accepting circle patch as fargs

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()

The print statement inside the for loop is displaying the states, however the print statement inside the animate function shows nothing, meaning that the function is not being called. for 循环中的打印语句显示状态,但是 animate 函数中的打印语句没有显示任何内容,这意味着该函数没有被调用。 I can't find any logical errors.我找不到任何逻辑错误。 Please help.请帮忙。

If you want to animate several elements together, you should not run several FuncAnimation .如果要将多个元素一起设置动画,则不应运行多个FuncAnimation Instead, run one animation, and animate all the artists in the single animate() function.相反,运行一个动画,并在单个animate()函数中为所有艺术家animate()

I could not run you code, but I tried to stay close to what I could understand of your code in this demonstration:我无法运行你的代码,但我试图在这个演示中接近我对你的代码的理解:

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM