Matplotlib as a well-known data visualization tool in Python, its official website also provides the source code used in the PYQT4, here is an example of application, in case of a rainy future.
1) Create GUI interface with QT Designer
The demo GUI interface, shown in 1, uses qframe as the container for placing the Matplotlib interface. Then call Pyuic4.bat-o ui_maindialog.py Maindialog.ui to compile the UI interface.
Figure 1 GUI Design interface
2) maindialog.py Program code
#!/usr/bin/env python#-*-coding:utf-8-*-ImportNumPy as NP fromPyqt4.qtcoreImport* fromPyqt4.qtguiImport* fromUi_maindialogImportUi_maindialog fromMatplotlib.backends.backend_qt4aggImportFigurecanvasqtagg as Figurecanvas # matplotlib support for PYQT4 fromMatplotlib.figureImport FigureclassMaindialog (Qdialog, ui_maindialog):def __init__(Self, parent=None): Super (Maindialog, self).__init__(parent) self.setupui (self) self._createfigures () self._createlayouts ()
# Create a matplotlib canvas def_createfigures (self): Self._fig= Figure (Figsize= (8, 6), dpi=100, tight_layout=True) Self._fig.set_facecolor ("#F5F5F5") # background color self._fig.subplots_adjust ( left=0.08, top=0.92, right=0.95, bottom=0.1) # Four weeks margin Self._canvas=Figurecanvas (self._fig) # canvas self._ax= Self._fig.add_subplot (111) # Add subplot self._ax.hold (True) self._initializefigure ()def_createlayouts (self): layout=qhboxlayout (self.frame) layout.setcontentsmargins (0, 0, 0, 0) layout.addwidget (self._canvas) # ADD MATP Lotlidef_initializefigure (self): Font= {'Family':'Tahoma', 'Weight':'Bold', 'size': 10} #AbscissaSelf._ax.set_xlim ([380, 780]) self._ax.set_xticks ([380, 460, 540, 620, 700, 780]) self._ax.set_xticklabels ([380, 460, 540, 620, 780], fontdict=Font) Self._ax.set_xlabel ("Wavelength (nm)", fontdict=Font)#OrdinateSelf._ax.set_ylim ([0.0, 1.0]) self._ax.set_yticks (Np.arange (0.0, 1.1, 0.2)) Self._ax.set_yticklabels (Np.arange (0.0, 1.1, 0.2), fontdict=Font) Self._ax.set_ylabel ("Spectral Radiance (w/(M$^2$*SR*NM))", fontdict=Font) Self._ax.grid (True)#Grid on def_updatefigures (self): Font= {'Family':'Tahoma', 'Weight':'Bold', 'size': 10} self._ax.clear () Maxy= 0.0x= Np.arange (380, 781) y= Np.random.rand (401) Self._ax.plot (x, Y,'R', label="Data")
Maxy = max (y)ifMaxy <=0:self._initializefigure ()Else: Self._fig.subplots_adjust ( left=0.11, top=0.92, right=0.95, bottom=0.1) #AbscissaSelf._ax.set_xlim ([380, 780]) self._ax.set_xticks ([380, 460, 540, 620, 700, 780]) self._ax.set_xticklabels ([380, 460, 540, 620, 780], fontdict=Font) Self._ax.set_xlabel ("Wavelength (nm)", fontdict=Font)#OrdinateSelf._ax.set_ylim ([0.0, Maxy]) Self._ax.set_yticks ([0.0, maxy/4.0, maxy/2.0, Maxy * 3/4.0, Maxy]) Self._ax.set_yticklabels (["%.1e"% 0.0,"%.1e"% (maxy/4.0),"%.1e"% (maxy/2.0),"%.1e"% (Maxy * 3.0/4.0), "%.1e"% Maxy], fontdict=Font) Self._ax.set_ylabel ("Spectral Radiance (w/(M$^2$*SR*NM))", fontdict=Font) Self._ax.grid (True) self._ax.legend (Loc=" Best", fontsize="Small"). Draggable (State=true)#LegendSelf._canvas.draw () @pyqtSlot ( )defon_plotpushbutton_clicked (self): Self._updatefigures ()
The initial interface 2 shows:
Figure 2 GUI Initial interface
3) After clicking the plot button
The interface is shown in Figure 3:
Figure 3 Click the Plot button after the interface
Application of Matplotlib in PYQT4