gpt4 book ai didi

python - 如何在 Matplotlib 中与线程一起执行动画?

转载 作者:行者123 更新时间:2023-12-01 08:07:58 26 4
gpt4 key购买 nike

我想通过线程每 0.25 秒更改一次圆圈颜色,并通过 matplotlib 动画实时显示结果。这是我的代码(我什至不明白为什么不执行动画):

import threading
import time

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.animation import FuncAnimation

def apply_color_shift(fig, circle):

def func():
for i in range(100):
circle.set_fc((i/100, 0, 0, 1))
time.sleep(0.25)
print("something")
anim = threading.Thread(target = func)

def do_nothing(frame):
print("ANIM")
fig.show()
FuncAnimation(fig, do_nothing, frames = [0.1*i for i in range(100)])

anim.start()
plt.show()

fig, ax = plt.subplots()
ax.axis('square')

c = Circle(xy = (0, 0), color = "red")
ax.add_patch(c)
ax.set_xlim([-50, 50])
ax.set_ylim([-50, 50])

fig.show()
apply_color_shift(fig, c)

这里有什么问题,如何解决?

最佳答案

如果你想每0.25秒改变一次颜色,那应该是动画的间隔:

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
ax.axis('square')

c = Circle(xy = (0, 0), color = "red")
ax.add_patch(c)
ax.set_xlim([-50, 50])
ax.set_ylim([-50, 50])

def change_color(i):
c.set_fc((i/100, 0, 0, 1))

ani = FuncAnimation(fig, change_color, frames = range(100), interval=250)

plt.show()

关于python - 如何在 Matplotlib 中与线程一起执行动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55443690/

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