简体   繁体   中英

Adding a toolbar to a MPL figure in PyQt4

I have a very simple pyqt4 application that embeds a MatPlotLib figure. I am embedding the matplotlib figure through the MatplotlibWidget and I created the interface through the QtDesigner along with pyuic4 .

I would like to provide the user access to the toolbar for interactive navigation . But, despite their nice example on GTK, I can't seem to get it to work for pyQt. It mentions examples, but the example for QT4 provided does not include the toolbar.

I appreciate any help with this.


This question is similar, but does not quite address what I need and I have not been able to adapt it.

There is no toolbar widget in QtDesigner, but you can add the toolbar by code:

Here is the example, the plot_layout is a QVBoxLayout designed by QtDesigner, and plot_canvas is the MatplotlibWidget widget.

import numpy as np
from PyQt4.QtCore import Qt
from PyQt4.QtGui import *
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
from plot_dialog2 import Ui_Form

class PlotDialog(QWidget, Ui_Form):
    def __init__(self):
        QWidget.__init__(self)
        self.setupUi(self)
        self.navi_toolbar = NavigationToolbar(self.plot_canvas, self)
        self.plot_layout.addWidget(self.navi_toolbar)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    dialog = PlotDialog()
    dialog.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM