matplotlib绑定到PyQt5(有菜单)

稍微复杂地实现matplotlib绑定到PyQt5(有菜单)

【知识点】

 import matplotlib
 matplotlib.use("Qt5Agg")

【效果图】

【源代码】

  1 import sys
  2 import random
  3
  4 import matplotlib
  5 matplotlib.use("Qt5Agg")
  6
  7 from PyQt5 import QtCore
  8 from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget
  9
 10 from numpy import arange, sin, pi
 11 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
 12 from matplotlib.figure import Figure
 13
 14
 15 class MyMplCanvas(FigureCanvas):
 16     """这是一个窗口部件,即QWidget(当然也是FigureCanvasAgg)"""
 17     def __init__(self, parent=None, width=5, height=4, dpi=100):
 18         fig = Figure(figsize=(width, height), dpi=dpi)
 19         self.axes = fig.add_subplot(111)
 20         # 每次plot()调用的时候,我们希望原来的坐标轴被清除(所以False)
 21         self.axes.hold(False)
 22
 23         self.compute_initial_figure()
 24
 25         #
 26         FigureCanvas.__init__(self, fig)
 27         self.setParent(parent)
 28
 29         FigureCanvas.setSizePolicy(self,
 30                                    QSizePolicy.Expanding,
 31                                    QSizePolicy.Expanding)
 32         FigureCanvas.updateGeometry(self)
 33
 34     def compute_initial_figure(self):
 35         pass
 36
 37 class MyStaticMplCanvas(MyMplCanvas):
 38     """静态画布:一条正弦线"""
 39     def compute_initial_figure(self):
 40         t = arange(0.0, 3.0, 0.01)
 41         s = sin(2*pi*t)
 42         self.axes.plot(t, s)
 43
 44
 45 class MyDynamicMplCanvas(MyMplCanvas):
 46     """动态画布:每秒自动更新,更换一条折线。"""
 47     def __init__(self, *args, **kwargs):
 48         MyMplCanvas.__init__(self, *args, **kwargs)
 49         timer = QtCore.QTimer(self)
 50         timer.timeout.connect(self.update_figure)
 51         timer.start(1000)
 52
 53     def compute_initial_figure(self):
 54         self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], ‘r‘)
 55
 56     def update_figure(self):
 57         # 构建4个随机整数,位于闭区间[0, 10]
 58         l = [random.randint(0, 10) for i in range(4)]
 59
 60         self.axes.plot([0, 1, 2, 3], l, ‘r‘)
 61         self.draw()
 62
 63 class ApplicationWindow(QMainWindow):
 64     def __init__(self):
 65         QMainWindow.__init__(self)
 66         self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
 67         self.setWindowTitle("程序主窗口")
 68
 69         self.file_menu = QMenu(‘&File‘, self)
 70         self.file_menu.addAction(‘&Quit‘, self.fileQuit,
 71                                  QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
 72         self.menuBar().addMenu(self.file_menu)
 73
 74         self.help_menu = QMenu(‘&Help‘, self)
 75         self.menuBar().addSeparator()
 76         self.menuBar().addMenu(self.help_menu)
 77
 78         self.help_menu.addAction(‘&About‘, self.about)
 79
 80         self.main_widget = QWidget(self)
 81
 82         l = QVBoxLayout(self.main_widget)
 83         sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)
 84         dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
 85         l.addWidget(sc)
 86         l.addWidget(dc)
 87
 88         self.main_widget.setFocus()
 89         self.setCentralWidget(self.main_widget)
 90         # 状态条显示2秒
 91         self.statusBar().showMessage("matplotlib 万岁!", 2000)
 92
 93     def fileQuit(self):
 94         self.close()
 95
 96     def closeEvent(self, ce):
 97         self.fileQuit()
 98
 99     def about(self):
100         QMessageBox.about(self, "About",
101         """embedding_in_qt5.py example
102         Copyright 2015 BoxControL
103
104         This program is a simple example of a Qt5 application embedding matplotlib
105         canvases. It is base on example from matplolib documentation, and initially was
106         developed from Florent Rougon and Darren Dale.
107
108         http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html
109
110         It may be used and modified with no restriction; raw copies as well as
111         modified versions may be distributed without limitation.
112         """
113         )
114
115 if __name__ == ‘__main__‘:
116     app = QApplication(sys.argv)
117
118     aw = ApplicationWindow()
119     aw.setWindowTitle("PyQt5 与 Matplotlib 例子")
120     aw.show()
121     #sys.exit(qApp.exec_())
122     app.exec_()
时间: 2024-10-23 15:19:16

matplotlib绑定到PyQt5(有菜单)的相关文章

matplotlib绑定到PyQt5(无菜单)

很简单的实现matplotlib绑定到PyQt5 [知识点] 1 import matplotlib 2 matplotlib.use("Qt5Agg") 3 4 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 5 from matplotlib.figure import Figure [效果图] [源代码] 1 import numpy as np 2 import m

datatable绑定comboBox,在下拉菜单中显示对应数据

实现功能: datatable绑定comboBox,在下拉菜单中显示对应数据 实现方法: 1.生成datatable,并为combox绑定数据源: comboBox1.DataSource = dt1; comboBox1.DisplayMember = "用户编码"; comboBox1.ValueMember = "ID"; this.comboBox1.SelectedIndex = -1; 2.在combox的SelectedIndexChanged事件中添

Matplotlib植入PyQt5 + QT5的UI呈现

实现matplotlib图形通过PyQt5+Qt5在GUI中呈现步骤: 第一步,通过matplotlib.backends.backend_qt5agg类来连接PyQt5: 1 import matplotlib 2 matplotlib.use("Qt5Agg") # 声明使用QT5 3 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 4 from matplotlib.

从零开始编写自己的C#框架(18)——Web层后端权限模块——菜单管理

从本章开始,主要讲解的是页面中对框架相关功能的调用方法,比如列表页面(又分为有层次感列表和普通列表).编辑页面.多标签页面等,只要熟悉了这些函数的使用方法,那么开发起来就会很便捷了. 1.如图先创建菜单列表与编辑页面 MenuInfoList.aspx 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MenuInfoList.aspx.cs" Inherits=&quo

matplotlib 代码风格

当查看 matplotlib 官方文档和示例时,你会发现不同的编码风格和使用模式.这些风格是完全有效的,各有其优缺点. 几乎所有的例子都可以转换成另一种风格,以实现相同的结果.当然,需要注意的是不要把自己的代码风格搞混. 两种不同的风格,都受官方支持. 下面是使用 matplotlib 的首选方式.首选 pyplot 样式,顶部的导入脚本通常是: import matplotlib.pyplot as plt import numpy as np 风格一:直接调用绘图函数,下面是一个简单的例子:

datatable绑定comboBox显示数据[C#]

实现功能: datatable绑定comboBox,在下拉菜单中显示对应数据 实现方法: //dataSet 转 dataTable 1.生成datatable,并为combox绑定数据源: comboBox1.DataSource = dt1; comboBox1.DisplayMember = "用户编码"; comboBox1.ValueMember = "ID"; this.comboBox1.SelectedIndex = -1; 2.在combox的Se

C# WPF侧边栏导航菜单(Dropdown Menu)

时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.WPF.ASP.NET Core等,亦有C++桌面相关的Qt Quick和Qt Widgets等,只分享自己熟悉的.自己会的. 阅读导航: 一.先看效果 二.本文背景 三.代码实现 四.文章参考 五.代码下载 一.先看效果 二.本文背景 YouTube  Design com WPF 大神处习得,菜单

WPF动态加载Menu菜单

有一个项目动态加载wpf的菜单控件可以非常有效的做到更具用户权限显示或控制MenuItem菜单,如何实现这种功能,其实也非常简单. 首先需要了解Wpf的menu菜单控件,她的结构其实也非常简单 <Menu DockPanel.Dock="Top" Name="MenuOptionGroup">   <MenuItem Header="菜单1">   <MenuItem Header="内容1"&g

wxpython学习资源

http://www.cnblogs.com/dyx1024/archive/2012/07/15/2592202.html wxPython:布局管理器sizer介绍 ogs.com/dyx1024/ date:2012-07-15 ''' import wx import...以本节看一下wxPython中布局管理器sizer的用法,同样,先看一个实例: 代码: #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Function:绘图 soc