[HP3562A] replace matplotlib with pytgraph

Wed, 16 May 2018 09:10:55 +0200

author
David Douard <david.douard@logilab.fr>
date
Wed, 16 May 2018 09:10:55 +0200
changeset 90
869de27dedc7
parent 89
61074a32808a
child 91
f2a8f688dbc0

[HP3562A] replace matplotlib with pytgraph

pygpibtoolkit/HP3562A/datablockwidget.py file | annotate | diff | comparison | revisions
pygpibtoolkit/qt5/mpl.py file | annotate | diff | comparison | revisions
setup.py file | annotate | diff | comparison | revisions
--- a/pygpibtoolkit/HP3562A/datablockwidget.py	Tue May 15 16:09:16 2018 +0200
+++ b/pygpibtoolkit/HP3562A/datablockwidget.py	Wed May 16 09:10:55 2018 +0200
@@ -20,8 +20,9 @@
 from PyQt5 import QtCore, QtWidgets
 from PyQt5.QtCore import Qt
 
+from pyqtgraph import PlotWidget
 
-from pygpibtoolkit.qt4.mpl import QMplCanvas, FigureCanvas
+#from pygpibtoolkit.qt5.mpl import QMplCanvas
 
 from pygpibtoolkit.HP3562A import state_decoder
 from pygpibtoolkit.HP3562A import trace_decoder
@@ -229,11 +230,13 @@
 
     def setupUI(self):
         self.setupToolBar()
-        self.canvas = QMplCanvas(self)
         super().setupUI()
         mainw = QtWidgets.QWidget(self)
         l = QtWidgets.QVBoxLayout(mainw)
         l.setContentsMargins(0, 0, 0, 0)
+        self.canvas = PlotWidget(self)
+        self.plot = self.canvas.plot()
+        self.canvas.showGrid(x=True, y=True)
         l.addWidget(self.canvas, 1)
 
         self.setCentralWidget(mainw)
@@ -248,19 +251,14 @@
             minv = min(y[y > 0])
             y[y == 0] = minv
             y = numpy.log10(y)
-            y = y*10
-        self.canvas.axes.clear()
-        self.canvas.axes.plot(x, y, 'r')
-        self.canvas.axes.set_xlim(min(x), max(x))
-        self.canvas.axes.set_xlabel("%s (%s)"%(self.header[0]['Domain'],
-                                               self.header[0]['X axis units']))
-        if self.ylogaction.isChecked():
-            self.canvas.axes.set_ylabel('db (%s)'%self.header[0]['Amplitude units'])
-        else:
-            self.canvas.axes.set_ylabel('%s'%self.header[0]['Amplitude units'])
-        self.canvas.axes.grid(True)
-        if self.header[0]['Display function']:
-            self.canvas.axes.set_title(self.header[0]['Display function'])
+            y = y * 10
+        self.plot.setData(x=x, y=y)
+        self.canvas.setLabel('bottom', self.header[0]['Domain'],
+                             units=self.header[0]['X axis units'])
+        yunit = 'dB' if self.ylogaction.isChecked() else ''
+        self.canvas.setLabel('left', self.header[0]['Amplitude units'],
+                             units=yunit)
+        self.canvas.setTitle(self.header[0]['Display function'])
 
         # compute THD, if any
         y = self.trace.copy()
@@ -285,9 +283,8 @@
         else:
             msg += 'THD+N:%.2g db  ' % thdn
         self.statusBar().showMessage(msg)
-        self.canvas.draw() 
-        
-        
+
+
 class CoordBinaryDatablockMDIChild(TraceBinaryDatablockMDIChild):
     _username = "Coord"
     _header_struct = (coord_decoder.TRACE_HEADER, coord_decoder.HEADER, )
@@ -323,20 +320,17 @@
 
         if self.header[1]['Y scale factor']:
             y *= self.header[1]['Y scale factor']
-        self.canvas.axes.clear()
-        self.canvas.axes.plot(x, y, 'r')
-        self.canvas.axes.set_xlim(min(x), max(x))
-        self.canvas.axes.set_xlabel("%s (%s)"%(self.header[0]['Domain'],
-                                               self.header[0]['X axis units']))
-        self.canvas.axes.set_ylabel('%s (%s)'%(self.header[1]['Y coordinates'],
-                                               self.header[0]['Amplitude units']))
-        self.canvas.axes.grid(True)
-        if self.header[0]['Display function']:
-            self.canvas.axes.set_title(self.header[0]['Display function'])
-        
-        self.canvas.draw() 
-        
-    
+        self.plot.setData(x=x, y=y)
+        self.canvas.setLabel(
+            'bottom', self.header[0]['Domain'],
+            units=self.header[0]['X axis units'])
+        self.canvas.setLabel(
+            'left', self.header[1]['Y coordinates'],
+            units=self.header[0]['Amplitude units'])
+        self.canvas.showGrid(x=True, y=True)
+        self.canvas.setTitle(self.header[0]['Display function'])
+
+
 children.append(CoordBinaryDatablockMDIChild)
 children.append(TraceBinaryDatablockMDIChild)
 children.append(StateBinaryDatablockMDIChild)
--- a/pygpibtoolkit/qt5/mpl.py	Tue May 15 16:09:16 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-#!/usr/bin/env python
-# This program is free software; you can redistribute it and/or modify it under
-# the terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
-#
-# This program is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
-#
-# You should have received a copy of the GNU General Public License along with
-# this program; if not, write to the Free Software Foundation, Inc.,
-# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-
-# stolen from:
-# embedding_in_qt4.py --- Simple Qt4 application embedding matplotlib canvases
-#
-# Copyright (C) 2005 Florent Rougon
-#               2006 Darren Dale
-#
-# This file is an example program for matplotlib. It may be used and
-# modified with no restriction; raw copies as well as modified versions
-# may be distributed without limitation.
-
-""" Copyright (c) 2007-2018 David Douard (Paris, FRANCE).
-http://www.logilab.org/project/pygpibtoolkit -- mailto:david.douard@sdfa3.org
-"""
-
-from PyQt5 import QtCore, QtWidgets
-
-from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
-from matplotlib.figure import Figure
-
-
-class QMplCanvas(Canvas):
-    def __init__(self, parent=None, width=5, height=4, dpi=100):
-        self.fig = Figure(figsize=(width, height), dpi=dpi)
-        self.axes = self.fig.add_subplot(111)
-        # We want the axes cleared every time plot() is called
-        self.axes.hold(False)
-
-        super().__init__(self.fig)
-        self.setParent(parent)
-
-        self.setSizePolicy(QtWidgets.QSizePolicy.Expanding,
-                           QtWidgets.QSizePolicy.Expanding)
-        self.updateGeometry()
-
-    def sizeHint(self):
-        w, h = self.get_width_height()
-        return QtCore.QSize(w, h)
-
-    def minimumSizeHint(self):
-        return QtCore.QSize(10, 10)
-
-    def plot(self, xdata, ydata, color="r"):
-        self.axes.plot(xdata, ydata, color)
-
-
-def main():
-    from PyQt5.QtWidgets import QApplication
-    app = QApplication([])
-    w = QMplCanvas()
-    w.plot([x for x in range(10)], [x*x for x in range(10)])
-    w.show()
-    app.exec_()
-
-
-if __name__ == '__main__':
-    main()
--- a/setup.py	Tue May 15 16:09:16 2018 +0200
+++ b/setup.py	Wed May 16 09:10:55 2018 +0200
@@ -6,7 +6,7 @@
       packages=find_packages(),
       version='0.1.0',
       install_requires=[
-          'pyserial', 'numpy', 'matplotlib'],
+          'pyserial', 'numpy', 'PyQt5', 'pyqtgraph'],
       entry_points={'console_scripts': [
           'pygpib-detect=pygpibtoolkit.detect:main',
           'hp3562-coord=pygpibtoolkit.HP3562A.coord_decoder:main',

mercurial