python - 使用 matplotlib 平滑动画
问题描述
昨天我开始了学习如何为函数制作动画来为我的工作做一些小项目的旅程。现在,我正在尝试编写一个简单的线性方程图,一次只显示一个点
from itertools import count
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('seaborn-paper')
x_vals = []
y_vals = []
index = count()
def animate(i):
x_vals = []
y_vals = []
x_vals*= 0
y_vals*= 0
var=next(index)
x_vals.append(var*0.05 % 10)
y_vals.append(var*0.05 % 10)
plt.cla()
plt.xlim(0,10)
plt.ylim(0,10)
plt.scatter(x_vals,y_vals)
ani = FuncAnimation(plt.gcf(), animate, interval=1)
plt.tight_layout()
plt.show()
请记住,对于我正在尝试做的事情,这是一个超级粗略的解决方案。我的问题是:有没有办法平滑我的点动画,使它看起来不波涛汹涌?
解决方案
我找到了一种简化代码的方法,可以帮助您加快速度。将 index 作为 frames 参数的函数传递无疑会提高速度。
index = count()
def animate(i):
var = next(index)
v = var*0.05 % 10
plt.cla()
plt.xlim(0,10)
plt.ylim(0,10)
plt.scatter(v,v)
ani = FuncAnimation(plt.gcf(), animate, frames = index, interval = 1)
plt.tight_layout()
plt.show()
推荐阅读
- kubernetes - Ingress 使用 clusterIP 类型公开服务
- java - ubuntu 上的 Java 模块路径命令
- firebase - Firebase 函数读取和写入 Firebase 实时数据库是否有助于 Firebase 实时下载?
- javascript - 如何让我的代码为用户授权和许可生成 JWT 令牌?
- mysql - 如何将txt文件中的数据导入mysql数据库
- regex - 如何使用 Notepad++(或类似工具)批量查找代码
- linux - 如何在 Linux 中评估 TCP 变体
- rest - 从 REST API 响应下载数据,但响应代码为 -1
- python - 通过按小时和天分组来绘制时间序列数据
- laravel - 使用 laravel 向 whatsapp 发送消息和文件