- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想弄清楚是否有任何方法可以更改现有 matplotlib FuncAnimation 的间隔。我希望能够根据用户输入调整动画的速度。
我发现了一个类似的问题 How do I change the interval between frames (python)? ,但由于没有得到答复,我想无论如何我都会问它。
我需要和拥有的一个最小的例子是:
"""
Based on Matplotlib Animation Example
author: Jake Vanderplas
https://stackoverflow.com/questions/35658472/animating-a-moving-dot
"""
from matplotlib import pyplot as plt
from matplotlib import animation
import Tkinter as tk
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
class AnimationWindow(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.fig = plt.figure(0, figsize=(10, 10))
self.anim = None
self.speed = 2
self.canvas = FigureCanvasTkAgg(self.fig, self)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.canvas.mpl_connect('resize_event', self.on_resize)
self.bar = tk.Scale(self, from_=0.25, to=10, resolution=0.25, command=self.change_play_speed, orient=tk.HORIZONTAL)
self.bar.pack(fill=tk.X)
def start_animation(self):
ax = plt.axes()
self.x = np.arange(0, 2 * np.pi, 0.01)
self.line, = ax.plot(self.x, np.sin(self.x))
# The return needs to be assigned to a variable in order to prevent the cleaning by the GC
self.anim = animation.FuncAnimation(self.fig, self.animation_update, frames=100,
interval=100/self.speed, blit=True, repeat=False)
def animation_update(self, i):
self.line.set_ydata(np.sin(self.x + i / 10.0)) # update the data
return self.line,
return tuple(self.annotation)
def change_play_speed(self, speed):
self.speed = float(speed)
# This works but I think somehow the previous animation remains
#self.anim = animation.FuncAnimation(self.fig, self.animation_update, frames=100, interval=100/self.speed, blit=True, repeat=False)
def on_resize(self, event):
"""This function runs when the window is resized.
It's used to clear the previous points from the animation which remain after resizing the windows."""
plt.cla()
def main():
root = tk.Tk()
rw = AnimationWindow(root)
rw.pack()
rw.start_animation()
root.mainloop()
if __name__ == '__main__':
main()
最佳答案
我不建议删除动画。更复杂的动画的一种选择当然是手动编程。使用重复调用更新函数的定时器实际上并不比创建 FuncAnimation
多多少代码。 .
但是,在这种情况下,解决方案非常简单。只需改变底层的间隔event_source
:
def change_play_speed(self, speed):
self.speed = float(speed)
self.anim.event_source.interval = 100./self.speed
关于python - 我可以更改之前创建的 matplotlib FuncAnimation 的间隔吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41882736/
我对此给出了答案thread ,谈论 matplotlib 上的褪色点。我对 ImportanceOfBeingErnest 的回答感到好奇。所以我尝试尝试他的代码。 首先,这是我的代码。 impor
我正在制作一个应该可视化传感器数据的应用程序。因此,我使用 python 和 tkinter 作为 gui 框架和 matplotlib 来可视化数据。该图应显示当前传感器值,因此它应该是动画的。但是
我对此给出了答案thread ,谈论 matplotlib 上的褪色点。我对 ImportanceOfBeingErnest 的回答感到好奇。所以我尝试尝试他的代码。 首先,这是我的代码。 impor
有谁知道停止 FuncAnimation 的首选方法吗?我用它来记录来自示波器的数据,并希望能够按需暂停和重新启动数据。有什么方法可以向它发送按钮点击事件吗? 谢谢,德里克 最佳答案 FuncAnim
下面的代码有效,但我认为每个重复周期都会过度绘制原始点。我希望它从原点开始,每个重复循环,有一个清晰的情节。在解决此问题的许多方法中,我尝试在 init 和 update 函数中插入 ax.clear
我试图从头开始模拟双星系统中行星的运动。为此,我需要能够在动画图中绘制点。在对整个事情进行编码之前,我正在学习使用 pyplot 为绘图设置动画。到目前为止,我还没有运气为移动点设置动画。在查看了几个
我正在尝试为机器人的链接设置动画但没有成功。 FuncAnimation 从不调用 animate 函数——从不执行打印语句。任何帮助将不胜感激。我的代码: def foo(): joints
我是 python 的新手,我正在尝试使用 matplotlibs FuncAnimation 制作动画并将其保存为某种视频格式。如果我运行以下代码: import numpy as np impor
我正在尝试用我计算的数据制作一部电影。我正在使用 ffmpeg 编写器。当我在 Spyder 中启动动画时,它工作正常并持续到最后,但当我尝试保存它时,它只持续动画的前 30%。我怎样才能保存整个动画
我想从不同的文件生成一系列动画线图的视频。下面是我的 python 代码的开始。我正在使用matplotlib.animation图书馆。 看起来像这样: 如何删除之前的绘图(一次显示一行),但保持相
我想构建一个带有某些实时图表的界面,显示一些实验的结果。为此,我决定结合使用glade(UI)、gtk、python 和 matplotlib。我正在学习一些基础知识,并且能够绘制一些实时图表。 现在
我正在尝试使用 FuncAnimation 模块制作动画,但我的代码只生成一帧然后停止。它似乎没有意识到它需要更新什么。你能帮我看看哪里出了问题吗? import numpy as np import
我使用 matplotlib 的 FuncAnimation 创建了表面绑定(bind)过程的简单动画。然而,结果非常缓慢。我怀疑这是因为我在每一帧重新绘制所有元素,但我还没有找到解决方法。任何帮助表
根据 matplotlib 的文档,FuncAnimation 重复调用一个函数来制作动画。 问题是,当没有更多图像数据可用时,我应该如何告诉 FuncAnimation() 停止? 最佳答案 如果您
如何将参数传递给animation.FuncAnimation()?我试过了,但没用。 animation.FuncAnimation() 的签名是 class matplotlib.animatio
浏览了一些关于 matplotlib 动画的教程并遇到了这个问题。我正在使用 matplotlib.animation funcanimation 如下: import matplotlib.anim
我正在尝试使用 Matplotlib 的 FuncAnimation 来动画显示每帧动画中的一个点。 # modules #--------------------------------------
我想弄清楚是否有任何方法可以更改现有 matplotlib FuncAnimation 的间隔。我希望能够根据用户输入调整动画的速度。 我发现了一个类似的问题 How do I change the
我在 Matplotlib 中遇到动画缓慢的问题。我正在对模拟结果进行动画处理,这是最简单的可视化方式,其中包含一系列随时间改变颜色的矩形。 遵循建议 here ,我使用 blitting 仅绘制每帧
我制作了一个脚本,它使用 matplotlib 的 FuncAnimation 函数来为抛物面函数的一系列等高线图设置动画。我想添加一个颜色条,其范围在整个动画过程中都不会改变。我真的不知道该怎么做。
我是一名优秀的程序员,十分优秀!