matplotlib绑定到PyQt5(有菜单)
稍微复杂地实现matplotlib绑定到PyQt5(有菜单)
【知识点】
import matplotlib
matplotlib.use("Qt5Agg")
【效果图】
【源代码】
import sys
import random import matplotlib
matplotlib.use("Qt5Agg") from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure class MyMplCanvas(FigureCanvas):
"""这是一个窗口部件,即QWidget(当然也是FigureCanvasAgg)"""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# 每次plot()调用的时候,我们希望原来的坐标轴被清除(所以False)
self.axes.hold(False) self.compute_initial_figure() #
FigureCanvas.__init__(self, fig)
self.setParent(parent) FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self) def compute_initial_figure(self):
pass class MyStaticMplCanvas(MyMplCanvas):
"""静态画布:一条正弦线"""
def compute_initial_figure(self):
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
self.axes.plot(t, s) class MyDynamicMplCanvas(MyMplCanvas):
"""动态画布:每秒自动更新,更换一条折线。"""
def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.update_figure)
timer.start(1000) def compute_initial_figure(self):
self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r') def update_figure(self):
# 构建4个随机整数,位于闭区间[0, 10]
l = [random.randint(0, 10) for i in range(4)] self.axes.plot([0, 1, 2, 3], l, 'r')
self.draw() class ApplicationWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("程序主窗口") self.file_menu = QMenu('&File', self)
self.file_menu.addAction('&Quit', self.fileQuit,
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
self.menuBar().addMenu(self.file_menu) self.help_menu = QMenu('&Help', self)
self.menuBar().addSeparator()
self.menuBar().addMenu(self.help_menu) self.help_menu.addAction('&About', self.about) self.main_widget = QWidget(self) l = QVBoxLayout(self.main_widget)
sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)
dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
l.addWidget(sc)
l.addWidget(dc) self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
# 状态条显示2秒
self.statusBar().showMessage("matplotlib 万岁!", 2000) def fileQuit(self):
self.close() def closeEvent(self, ce):
self.fileQuit() def about(self):
QMessageBox.about(self, "About",
"""embedding_in_qt5.py example
Copyright 2015 BoxControL This program is a simple example of a Qt5 application embedding matplotlib
canvases. It is base on example from matplolib documentation, and initially was
developed from Florent Rougon and Darren Dale. http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html It may be used and modified with no restriction; raw copies as well as
modified versions may be distributed without limitation.
"""
) if __name__ == '__main__':
app = QApplication(sys.argv) aw = ApplicationWindow()
aw.setWindowTitle("PyQt5 与 Matplotlib 例子")
aw.show()
#sys.exit(qApp.exec_())
app.exec_()
matplotlib绑定到PyQt5(有菜单)的更多相关文章
- matplotlib绑定到PyQt5(无菜单)
很简单的实现matplotlib绑定到PyQt5 [知识点] import matplotlib matplotlib.use("Qt5Agg") from matplotlib. ...
- datatable绑定comboBox,在下拉菜单中显示对应数据
实现功能: datatable绑定comboBox,在下拉菜单中显示对应数据 实现方法: .生成datatable,并为combox绑定数据源: comboBox1.DataSource = dt1; ...
- PyQt5 各种菜单实现
# -*- coding: utf-8 -*- # Created by PCITZDF on 2018/4/8 15:36. # FileName: menuandtools.py import s ...
- PyQt5教程——菜单和工具栏(3)
PyQt5中的菜单和工具栏 在这部分的PyQt5教程中,我们将创建菜单和工具栏.菜单式位于菜单栏的一组命令操作.工具栏是应用窗体中由按钮和一些常规命令操作组成的组件. 主窗口 QMainWindow类 ...
- Matplotlib植入PyQt5 + QT5的UI呈现
实现matplotlib图形通过PyQt5+Qt5在GUI中呈现步骤: 第一步,通过matplotlib.backends.backend_qt5agg类来连接PyQt5: import matplo ...
- PIE SDK图层树右键菜单与命令绑定
1. 功能简介 上一节已经介绍过图层树如何和地图和制图关联,图层树右键菜单主要是基于TocControl控件进行对菜单节点进行控制,TocControl主要作用是显示当前加载的图层有哪些.采用什么 ...
- 从零开始编写自己的C#框架(18)——Web层后端权限模块——菜单管理
从本章开始,主要讲解的是页面中对框架相关功能的调用方法,比如列表页面(又分为有层次感列表和普通列表).编辑页面.多标签页面等,只要熟悉了这些函数的使用方法,那么开发起来就会很便捷了. 1.如图先创建菜 ...
- datatable绑定comboBox显示数据[C#]
实现功能: datatable绑定comboBox,在下拉菜单中显示对应数据 实现方法: 1.生成datatable,并为combox绑定数据源: comboBox1.DataSource = dt1 ...
- 可折叠的ToolBar+抽屉菜单NavigationView+浮动按钮FloatButton
使用Material Design风格的ToolBar和抽屉导航 先看个简单的运行效果 主要记录下布局的写法 1 用到的Google Design依赖和V7包依赖 compile 'com.andro ...
随机推荐
- socat 的神奇使用方式
目的是实现科* 学 * 上*网,现在记录一下流程 先在服务器上安装(比如美国,香港,台湾,马来的云主机)squid,easy_rsa, centos 下可以用yum直接安装 $ yum install ...
- apache软件no_ssl和openssl两种类型的区别
apache软件同一版本有两种类型:no_ssl和openssl: openssl多了个ssl安全认证模式,它的协议是HTTPS而不是HTTP,这就是带有SSL的服务器与一般网页服务器的区别了. 一般 ...
- beautifulsoup之CSS选择器
BeautifulSoup支持大部分的CSS选择器,其语法为:向tag或soup对象的.select()方法中传入字符串参数,选择的结果以列表形式返回. tag.select("string ...
- python入门の缩进魔术
idx=1 sum=0 while idx<=100: sum=idx+sum idx=idx+1 print('sum 100 =', sum) ======================= ...
- SQL点点滴滴_聪明的小写法(持续更新中)
1.生成序列号 SELECT number + 1 ,number FROM master..spt_values WHERE type = 'P' ORDER BY number 说明: maste ...
- 如何快速开发一个支持高效、高并发的分布式ID生成器
ID生成器是指能产生不重复ID服务的程序,在后台开发过程中,尤其是分布式服务.微服务程序开发过程中,经常会用到,例如,为用户的每个请求产生一个唯一ID.为每个消息产生一个ID等等,ID生成器也是进行无 ...
- PHP-7的FPM服务的启动
1.PHP 7的FPM服务位置:/ect/init.d/php7.0-fpm 2.启动:sudo service php7.0-fpm reload
- print(函数.__closure__) 来判断是不是闭包, 返回cell , 是闭包, 返回None 则不是闭包
print(函数.__closure__) 来判断是不是闭包, 返回cell , 是闭包, None 则不是闭包
- "函中函" -------------------- func2(func) -------------- 函数名可以当做函数的参数
def func(): print("吃了么")def func2(fn): print("我是func2") fn() # 执⾏传递过来的fn # 即 fn替 ...
- mysql分页(ajax)
分页有多种方式,mysql的limit是个不错的选择,通过ajax实现异步刷新,将当前页数和每页数量传入后台即可 1. 首先后台要拿到总记录数和所需显示数据列表,所以要分别写两个sql方法 我这里是通 ...