Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save monkeyxite/8234288 to your computer and use it in GitHub Desktop.
Save monkeyxite/8234288 to your computer and use it in GitHub Desktop.
matplotlib Qt widget plotting
from sys import argv
from PyQt4 import QtGui, QtCore
# Import the interface class
import main_window
#matplotlibwidget is inherit from matplotlibwidget in site-packages, which pointed by matplotlibplugin.py of Qt Desiner python plugin -- matplotplugin
#several options of this matplotlibwidget implementation as matplotlibwidget_withbar.py or mplwidget.py could apply as your demands
from matplotlibwidget import *
from numpy import linspace
class PlotViewer(QtGui.QMainWindow, main_window.Ui_MainWindow):
""" The second parent must be 'Ui_<obj. name of main widget class>'.
If confusing, simply open up ImageViewer.py and get the class
name used. I'd named mine as mainWindow, hence Ui_mainWindow. """
def __init__(self, parent=None):
super(PlotViewer, self).__init__(parent)
# This is because Python does not automatically
# call the parent's constructor.
self.setupUi(self)
# Pass this "self" for building widgets and
# keeping a reference.
self.mplwidget = MatplotlibWidget(self,title='Example',
xlabel='Linear scale',
ylabel='Log scale',
hold=True, yscale='log')
self.mplwidget.setFocus()
self.setCentralWidget(self.mplwidget)
self.plot(self.mplwidget.axes)
def plot(self, axes):
x = linspace(-10, 10)
axes.plot(x, x**2)
axes.plot(x, x**3)
def main(self):
self.show()
if __name__=='__main__':
app = QtGui.QApplication(argv)
plotViewer = PlotViewer()
plotViewer.main()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment