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

本文目的:展示 PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar。

本人搜遍所有网络无果,没办法,查看PyQt5源代码,最终才搞明白。。。特此留记。

〇、PyQt4 与 PyQt5 导入 NavigationToolbar 时的区别(去掉两个agg)

# PyQt4 版本(网传)
#from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar

# PyQt5 版本
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar

一、隐藏 matplotlib 工具条

import sys
from PyQt5 import QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

import random

class Window(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.figure = plt.figure()
        self.axes = self.figure.add_subplot(111)
        # We want the axes cleared every time plot() is called
        self.axes.hold(False)
        self.canvas = FigureCanvas(self.figure)

        self.toolbar = NavigationToolbar(self.canvas, self)
        self.toolbar.hide()

        # Just some button
        self.button1 = QtWidgets.QPushButton(‘Plot‘)
        self.button1.clicked.connect(self.plot)

        self.button2 = QtWidgets.QPushButton(‘Zoom‘)
        self.button2.clicked.connect(self.zoom)

        self.button3 = QtWidgets.QPushButton(‘Pan‘)
        self.button3.clicked.connect(self.pan)

        self.button4 = QtWidgets.QPushButton(‘Home‘)
        self.button4.clicked.connect(self.home)

        # set the layout
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)

        btnlayout = QtWidgets.QHBoxLayout()
        btnlayout.addWidget(self.button1)
        btnlayout.addWidget(self.button2)
        btnlayout.addWidget(self.button3)
        btnlayout.addWidget(self.button4)
        qw = QtWidgets.QWidget(self)
        qw.setLayout(btnlayout)
        layout.addWidget(qw)

        self.setLayout(layout)

    def home(self):
        self.toolbar.home()
    def zoom(self):
        self.toolbar.zoom()
    def pan(self):
        self.toolbar.pan()

    def plot(self):
        ‘‘‘ plot some random stuff ‘‘‘
        data = [random.random() for i in range(25)]
        self.axes.plot(data, ‘*-‘)
        self.canvas.draw()

if __name__ == ‘__main__‘:
    app = QtWidgets.QApplication(sys.argv)

    main = Window()
    main.setWindowTitle(‘Simple QTpy and MatplotLib example with Zoom/Pan‘)
    main.show()

    sys.exit(app.exec_())

二、显示 matplotlib 工具条

import sys, os, random

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

import matplotlib
matplotlib.use(‘Qt5Agg‘)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setWindowTitle(‘Demo: PyQt with matplotlib‘)

        self.create_menu()
        self.create_main_frame()
        self.create_status_bar()

        self.textbox.setText(‘1 2 3 4‘)
        self.on_draw()

    def save_plot(self):
        file_choices = "PNG (*.png)|*.png"

        path = QFileDialog.getSaveFileName(self,
                        ‘Save file‘, ‘‘,
                        file_choices)
        if path:
            self.canvas.print_figure(path, dpi=self.dpi)
            self.statusBar().showMessage(‘Saved to %s‘ % path, 2000)

    def on_about(self):
        msg = """ A demo of using PyQt with matplotlib:

         * Use the matplotlib navigation bar
         * Add values to the text box and press Enter (or click "Draw")
         * Show or hide the grid
         * Drag the slider to modify the width of the bars
         * Save the plot to a file using the File menu
         * Click on a bar to receive an informative message
        """
        QMessageBox.about(self, "About the demo", msg.strip())

    def on_pick(self, event):
        # The event received here is of the type
        # matplotlib.backend_bases.PickEvent
        #
        # It carries lots of information, of which we‘re using
        # only a small amount here.
        #
        box_points = event.artist.get_bbox().get_points()
        msg = "You‘ve clicked on a bar with coords:\n %s" % box_points

        QMessageBox.information(self, "Click!", msg)

    def on_draw(self):
        """ Redraws the figure
        """
        #str = unicode(self.textbox.text())
        self.data = list(map(int, self.textbox.text().split()))

        x = range(len(self.data))

        # clear the axes and redraw the plot anew
        #
        self.axes.clear()
        self.axes.grid(self.grid_cb.isChecked())

        self.axes.bar(
            left=x,
            height=self.data,
            width=self.slider.value() / 100.0,
            align=‘center‘,
            alpha=0.44,
            picker=5)

        self.canvas.draw()

    def create_main_frame(self):
        self.main_frame = QWidget()

        # Create the mpl Figure and FigCanvas objects.
        # 5x4 inches, 100 dots-per-inch
        #
        self.dpi = 100
        self.fig = Figure((5.0, 4.0), dpi=self.dpi)
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self.main_frame)

        # Since we have only one plot, we can use add_axes
        # instead of add_subplot, but then the subplot
        # configuration tool in the navigation toolbar wouldn‘t
        # work.
        #
        self.axes = self.fig.add_subplot(111)

        # Bind the ‘pick‘ event for clicking on one of the bars
        #
        self.canvas.mpl_connect(‘pick_event‘, self.on_pick)

        # Create the navigation toolbar, tied to the canvas
        #
        self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)

        # Other GUI controls
        #
        self.textbox = QLineEdit()
        self.textbox.setMinimumWidth(200)
        self.textbox.editingFinished.connect(self.on_draw)

        self.draw_button = QPushButton("&Draw")
        self.draw_button.clicked.connect(self.on_draw)

        self.grid_cb = QCheckBox("Show &Grid")
        self.grid_cb.setChecked(False)
        self.grid_cb.stateChanged.connect(self.on_draw) #int

        slider_label = QLabel(‘Bar width (%):‘)
        self.slider = QSlider(Qt.Horizontal)
        self.slider.setRange(1, 100)
        self.slider.setValue(20)
        self.slider.setTracking(True)
        self.slider.setTickPosition(QSlider.TicksBothSides)
        self.slider.valueChanged.connect(self.on_draw)#int

        #
        # Layout with box sizers
        #
        hbox = QHBoxLayout()

        for w in [  self.textbox, self.draw_button, self.grid_cb,
                    slider_label, self.slider]:
            hbox.addWidget(w)
            hbox.setAlignment(w, Qt.AlignVCenter)

        vbox = QVBoxLayout()
        vbox.addWidget(self.mpl_toolbar)
        vbox.addWidget(self.canvas)
        vbox.addLayout(hbox)

        self.main_frame.setLayout(vbox)
        self.setCentralWidget(self.main_frame)

    def create_status_bar(self):
        self.status_text = QLabel("This is a demo")
        self.statusBar().addWidget(self.status_text, 1)

    def create_menu(self):
        self.file_menu = self.menuBar().addMenu("&File")

        load_file_action = self.create_action("&Save plot",
            shortcut="Ctrl+S", slot=self.save_plot,
            tip="Save the plot")
        quit_action = self.create_action("&Quit", slot=self.close,
            shortcut="Ctrl+Q", tip="Close the application")

        self.add_actions(self.file_menu,
            (load_file_action, None, quit_action))

        self.help_menu = self.menuBar().addMenu("&Help")
        about_action = self.create_action("&About",
            shortcut=‘F1‘, slot=self.on_about,
            tip=‘About the demo‘)

        self.add_actions(self.help_menu, (about_action,))

    def add_actions(self, target, actions):
        for action in actions:
            if action is None:
                target.addSeparator()
            else:
                target.addAction(action)

    def create_action(  self, text, slot=None, shortcut=None,
                        icon=None, tip=None, checkable=False,
                        signal="triggered()"):
        action = QAction(text, self)
        if icon is not None:
            action.setIcon(QIcon(":/%s.png" % icon))
        if shortcut is not None:
            action.setShortcut(shortcut)
        if tip is not None:
            action.setToolTip(tip)
            action.setStatusTip(tip)
        if slot is not None:
            action.triggered.connect(slot)
        if checkable:
            action.setCheckable(True)
        return action

def main():
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()

if __name__ == "__main__":
    main()
    
时间: 2024-08-09 22:00:12

PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar的相关文章

C#winform两个或多个panel重叠布局时如何显示一个

现象:panel1和panel2或更多panel,叠放在一起,panel和panel2是同一层次的.panel1和panel2切换时只能显示一个panel,如何切换呢? 解决方法: 1.打开vs视图-其他窗口-文档大纲,看看层次关系,就可以看出最上面的panel是窗体里显示出来的.如何让panel2出现在窗体去编辑呢? 2.拖动panel2到panel1前面,即可编辑panel2

<input type="file" />浏览时只显示指定文件类型

<input type="file" />浏览时只显示指定文件类型 <input type="file" accept="application/msword" ><br><br>accept属性列表<br> 1.accept="application/msexcel"2.accept="application/msword"3.accept=&q

用Natvis定制C++对象在Visual Studio调试时如何显示

博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用Natvis定制C++对象在Visual Studio调试时如何显示.

引用项目外dll时不显示注释的解决方案

在引用项目外的dll时,显示类库中的注释可按以下步骤: 方法或变量用summary添加注释,如: /// <summary>发送post请求 /// </summary>        /// <param name="url">请求的url</param>        /// <param name="postString">发送到数据 例如:"name=xhan&password=1

鼠标移动到图片上时,显示大图片

HTML标签中的一部分,仅供参考 <tr> <td><input type="checkbox" class="checkbox1"/></td> <td>1002</td> <td>小猫咪</td> <td><img src="img/02.jpg" height="100" width="100&qu

ng1中 如何用双向绑定 实现单向绑定的初始时不显示双括号效果?

ng1中 如何用双向绑定 实现单向绑定(ng-bind就可以不显示{{}})的初始时不显示双括号效果? AngularJS 实例 页面加载时防止应用闪烁: <div ng-app=""> <p ng-cloak>{{ 5 + 5 }}</p> </div> 尝试一下 » 定义和用法 ng-cloak 指令用于在 AngularJS 应用在加载时防止 AngularJS 代码未加载完而出现的问题. AngularJS 应用在加载时,文档可能

asp.net中ashx生成验证码代码放在Linux(centos)主机上访问时无法显示问题

最近有个项目加入了验证码功能,就从自己博客以前的代码中找到直接使用,直接访问验证码页面报错如下: 源代码:asp.net中使用一般处理程序生成验证码 Application Exception System.ArgumentException The requested FontFamily could not be found [GDI+ status: FontFamilyNotFound] Description: HTTP 500.Error processing request. De

spark on yarn提交任务时一直显示ACCEPTED

spark on yarn提交任务时一直显示ACCEPTED,过一个小时后就会出现任务失败,但在提交时shell终端显示的日志并没有报错,logs文件夹中也没有日志产生.注:spark on yarn是不需要启动spark集群的,只需要在提交任务的机器配置spark就可以了,因为任务是由hadoop来执行的,spark只负责任务的提交. 任务提交命令为 bin/spark-submit --class org.apache.spark.examples.JavaWordCount\     --

CCLabelAtlas的宽度为奇数时的显示bug

遇到一个很郁闷的bug,CCLabelAtlas设置文字内容在ipad上和android上正常,就只有iphone怎么显示都不正常.后来把它宽度 + 1,然后就正常了.发现以前宽度设置为21px.23px都有问题,+1px变为22px就正常了   按上图设置,然后用ps将画布的宽度再+1,让文字选区居中就ok了 CCLabelAtlas的宽度为奇数时的显示bug