PyQt5嵌入matplotlib动画

时间:2021-08-22 09:23:44

PyQt5嵌入matplotlib动画

 # -*- coding: utf-8 -*-

 import sys
from PyQt5 import QtWidgets import numpy as np
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.animation import FuncAnimation class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# We want the axes cleared every time plot() is called
self.axes.hold(False) self.compute_initial_figure() #
FigureCanvas.__init__(self, fig)
self.setParent(parent) def compute_initial_figure(self):
pass class AnimationWidget(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self) vbox = QtWidgets.QVBoxLayout()
self.canvas = MyMplCanvas(self, width=5, height=4, dpi=100)
vbox.addWidget(self.canvas) hbox = QtWidgets.QHBoxLayout()
self.start_button = QtWidgets.QPushButton("start", self)
self.stop_button = QtWidgets.QPushButton("stop", self)
self.start_button.clicked.connect(self.on_start)
self.stop_button.clicked.connect(self.on_stop)
hbox.addWidget(self.start_button)
hbox.addWidget(self.stop_button)
vbox.addLayout(hbox)
self.setLayout(vbox) self.x = np.linspace(0, 5*np.pi, 400)
self.p = 0.0
self.y = np.sin(self.x + self.p)
self.line, = self.canvas.axes.plot(self.x, self.y, animated=True, lw=2) def update_line(self, i):
self.p += 0.1
y = np.sin(self.x + self.p)
self.line.set_ydata(y)
return [self.line] def on_start(self):
self.ani = FuncAnimation(self.canvas.figure, self.update_line,
blit=True, interval=25) def on_stop(self):
self.ani._stop() if __name__ == "__main__":
qApp = QtWidgets.QApplication(sys.argv)
aw = AnimationWidget()
aw.show()
sys.exit(qApp.exec_())

PyQt5嵌入matplotlib动画

PyQt5嵌入matplotlib动画的更多相关文章

  1. PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar

    本文目的:展示 PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar. 本人搜遍所有网络无果,没办法,查看PyQt5源代码,最终才搞明白...特此留记. 〇.Py ...

  2. pyqt5 QGraphicsView颜色动画问题(不兼容,运行不了动画)

    初学动画.无敌踩坑,资料真的是太少了.....本坑是一个大坑,只有解决方法,但实质原因仍不清楚 在一篇资料中了解到我们可以通过QGraphicsView来实现动画QPropertyAnimation ...

  3. matplotlib动画

    注意:要有动画效果,必须独立窗口:独立窗口的设置方法:https://www.cnblogs.com/liming19680104/p/10614070.html import matplotlib. ...

  4. [PyQt5]动态显示matplotlib作图(一)

    完整实例 import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePoli ...

  5. matplotlib绑定到PyQt5(有菜单)

    稍微复杂地实现matplotlib绑定到PyQt5(有菜单) [知识点] import matplotlib matplotlib.use("Qt5Agg") [效果图] [源代码 ...

  6. python 下 tinker、matplotlib 混合编程示例一个

    该例是实现了 Tinker 嵌入 matplotlib 所绘制的蜡烛图(k 线),数据是从 csv 读入的.花一下午做的,还很粗糙,仅供参考.python 代码如下: import matplotli ...

  7. pyqt中使用matplotlib绘制动态曲线

    一.项目背景: 看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个. 二.需求描述: 1)X轴显示时间点,显示长度为1分钟 ...

  8. pyqt中使用matplotlib绘制动态曲线 – pythonic

    一.项目背景: 看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个. 二.需求描述: 1)X轴显示时间点,显示长度为1分钟 ...

  9. matplotlib之pyplot 学习示例

    现在通过numpy和matplotlib.pyplot 在Python上实现科学计算和绘图,而且和matlab极为相像(效率差点,关键是方便简单) 这里有大量plots代码例子.  1. 简单的绘图( ...

随机推荐

  1. velocity中$springMacroRequestContext.getMessage($code)

    在Java国际化(i18n)中, vm页面显示内容需要使用 #springMessage("title") 实际运行时发现页面输出$springMacroRequestContex ...

  2. Windows Store App 图像

    在Windows应用商店应用中可以使用两种方法来显示图片,这两种方法分别为使用Image对象和使用ImageBrush对象.Image对象可以直接呈现一幅图像,而ImageBrush对象则可以用一幅图 ...

  3. GDB中文手册

    用GDB调试程序GDB概述 2使用GDB 5GDB中运行UNIX的shell程序 8在GDB中运行程序 8调试已运行的程序 两种方法: 9暂停 / 恢复程序运行 9一.设置断点(BreakPoint) ...

  4. 详解公用表表达式(CTE)

    简介 对于SELECT查询语句来说,通常情况下,为了使T-SQL代码更加简洁和可读,在一个查询中引用另外的结果集都是通过视图而不是子查询来进行分解的.但是,视图是作为系统对象存在数据库中,那对于结果集 ...

  5. uboot start.S分析

    一.概述   1.本文综述及特色  阅读uboot,start.S是第一个源程序文件,主要完成初始化看门狗.定时器.重定位(拷贝代码段到内存中).初始化堆栈.跳转到第二阶段等工作. 网上关于这些内容的 ...

  6. mariadb自带命令行客户端指令笔记

    mysql -H 主机IP -u 用户名 -p -p表示要输密码,不要直接输了,要回车后在程序里输入 显示数据库列表: show databases; 选择XX数据库: use XX; 显示数据库里的 ...

  7. 朴素的treap

    所谓Treap,就是一种二叉查找树,而我们知道二叉查找树,相对来说比较容易形成最坏的链表情况,所以我们有一种数据结构来防止二叉查找树出现最坏情况,那就是Treap. Treap=tree+heap,T ...

  8. 洛谷 P5304 [GXOI/GZOI2019]旅行者(最短路)

    洛谷:传送门 bzoj:传送门 参考资料: [1]:https://xht37.blog.luogu.org/p5304-gxoigzoi2019-lv-xing-zhe [2]:http://www ...

  9. 输出GPLT

    L1-023 输出GPLT (20 分)   给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符( ...

  10. HTTP 错误 500.XX - Internal Server Error 解决办法

    HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core 通知 未知 处理程序 尚未 ...