python - Matplotlib 动画图 - 图形在循环完成之前没有响应
问题描述
我正在尝试为我的两个向量 X,Y 通过循环更新的情节制作动画。我正在使用 FuncAnimation
. 我遇到的问题是图形会显示Not Responding
或空白,直到循环完成。
所以在循环期间,我会得到类似的东西:
但是,如果我停止循环或在最后,该数字会出现。
我已将图形后端设置为automatic
.
以下是代码示例:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def animate( intermediate_values):
x = [i for i in range(len(intermediate_values))]
y = intermediate_values
plt.cla()
plt.plot(x,y, label = '...')
plt.legend(loc = 'upper left')
plt.tight_layout()
x = []
y = []
#plt.ion()
for i in range(50):
x.append(i)
y.append(i)
ani = FuncAnimation(plt.gcf(), animate(y), interval = 50)
plt.tight_layout()
#plt.ioff()
plt.show()
解决方案
matplotlib中动画的结构是循环过程中不使用动画函数,而动画函数就是循环过程。设置初始图形后,动画功能将更新数据。
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = []
y = []
fig = plt.figure()
ax = plt.axes(xlim=(0,50), ylim=(0, 50))
line, = ax.plot([], [], 'b-', lw=3, label='...')
ax.legend(loc='upper left')
def animate(i):
x.append(i)
y.append(i)
line.set_data(x, y)
return line,
ani = FuncAnimation(fig, animate, frames=50, interval=50, repeat=False)
plt.show()
推荐阅读
- sql - SQL 帮助 - 尝试标记用相反符号补充相同数字的值
- ethereum - 带有以太坊 ERC-20 代币的代币门
- android - 如何修复此错误“Gradle View 正在运行且未结束”
- pytorch - PyTorch:我可以按长度对批次进行分组吗?
- docker - ELASTIC_PASSWORD_FILE 在 Kibana 容器中不起作用
- javascript - 如何在一个数字范围内找到素数的数量
- python - 使用熊猫获取多列的持续时间
- nestjs - 被赋予了在 NestJS 中实现 memcached 的任务,创建一个实现 memcached 的添加、更新、获取和删除功能的可重用函数
- julia - 如何确定 Julia 中是否已采用文件系统路径
- python - 从两个不同的熊猫输出创建堆栈栏