mat_data_plot.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2011, Ye Cheng, Dorian Scholz
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of the TU Darmstadt nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from python_qt_binding import QT_BINDING, QT_BINDING_VERSION
34 
35 try:
36  from pkg_resources import parse_version
37 except:
38  import re
39 
40  def parse_version(s):
41  return [int(x) for x in re.sub(r'(\.0+)*$', '', s).split('.')]
42 
43 if QT_BINDING == 'pyside':
44  qt_binding_version = QT_BINDING_VERSION.replace('~', '-')
45  if parse_version(qt_binding_version) <= parse_version('1.1.2'):
46  raise ImportError('A PySide version newer than 1.1.0 is required.')
47 
48 from python_qt_binding.QtCore import Slot, Qt, qVersion, qWarning, Signal
49 from python_qt_binding.QtGui import QColor
50 from python_qt_binding.QtWidgets import QWidget, QVBoxLayout, QSizePolicy
51 
52 import operator
53 import matplotlib
54 if qVersion().startswith('5.'):
55  if QT_BINDING == 'pyside':
56  if parse_version(matplotlib.__version__) < parse_version('2.1.0'):
57  raise ImportError('A newer matplotlib is required (at least 2.1.0 for PySide 2)')
58  if parse_version(matplotlib.__version__) < parse_version('1.4.0'):
59  raise ImportError('A newer matplotlib is required (at least 1.4.0 for Qt 5)')
60  try:
61  matplotlib.use('Qt5Agg')
62  from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
63  except ImportError:
64  # work around bug in dateutil
65  import sys
66  import thread
67  sys.modules['_thread'] = thread
68  from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
69  from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
70 elif qVersion().startswith('4.'):
71  if parse_version(matplotlib.__version__) < parse_version('1.1.0'):
72  raise ImportError('A newer matplotlib is required (at least 1.1.0 for Qt 4)')
73  try:
74  matplotlib.use('Qt4Agg')
75  from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
76  except ImportError:
77  # work around bug in dateutil
78  import sys
79  import thread
80  sys.modules['_thread'] = thread
81  from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
82  try:
83  from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
84  except ImportError:
85  from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
86 else:
87  raise NotImplementedError('Unsupport Qt version: %s' % qVersion())
88 
89 from matplotlib.figure import Figure
90 
91 import numpy
92 
93 
94 class MatDataPlot(QWidget):
95 
96  class Canvas(FigureCanvas):
97 
98  """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
99 
100  def __init__(self, parent=None):
101  super(MatDataPlot.Canvas, self).__init__(Figure())
102  self.axes = self.figure.add_subplot(111)
103  self.axes.grid(True, color='gray')
104  self.figure.tight_layout()
105  self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
106  self.updateGeometry()
107 
108  def resizeEvent(self, event):
109  super(MatDataPlot.Canvas, self).resizeEvent(event)
110  self.figure.tight_layout()
111 
112  limits_changed = Signal()
113 
114  def __init__(self, parent=None):
115  super(MatDataPlot, self).__init__(parent)
117  self._toolbar = NavigationToolbar(self._canvas, self._canvas)
118  vbox = QVBoxLayout()
119  vbox.addWidget(self._toolbar)
120  vbox.addWidget(self._canvas)
121  self.setLayout(vbox)
122 
123  self._curves = {}
124  self._current_vline = None
125  self._canvas.mpl_connect('button_release_event', self._limits_changed)
126 
127  def _limits_changed(self, event):
128  self.limits_changed.emit()
129 
130  def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False):
131 
132  # adding an empty curve and change the limits, so save and restore them
133  x_limits = self.get_xlim()
134  y_limits = self.get_ylim()
135  if markers_on:
136  marker_size = 3
137  else:
138  marker_size = 0
139  line = self._canvas.axes.plot([], [], 'o-', markersize=marker_size, label=curve_name,
140  linewidth=1, picker=5, color=curve_color.name())[0]
141  self._curves[curve_id] = line
142  self._update_legend()
143  self.set_xlim(x_limits)
144  self.set_ylim(y_limits)
145 
146  def remove_curve(self, curve_id):
147  curve_id = str(curve_id)
148  if curve_id in self._curves:
149  self._curves[curve_id].remove()
150  del self._curves[curve_id]
151  self._update_legend()
152 
153  def _update_legend(self):
154  handles, labels = self._canvas.axes.get_legend_handles_labels()
155  if handles:
156  hl = sorted(zip(handles, labels), key=operator.itemgetter(1))
157  handles, labels = zip(*hl)
158  self._canvas.axes.legend(handles, labels, loc='upper left')
159 
160  def set_values(self, curve, data_x, data_y):
161  line = self._curves[curve]
162  line.set_data(data_x, data_y)
163 
164  def redraw(self):
165  self._canvas.axes.grid(True, color='gray')
166  self._canvas.draw()
167 
168  def vline(self, x, color):
169  # convert color range from (0,255) to (0,1.0)
170  matcolor = (color[0] / 255.0, color[1] / 255.0, color[2] / 255.0)
171  if self._current_vline:
172  self._current_vline.remove()
173  self._current_vline = self._canvas.axes.axvline(x=x, color=matcolor)
174 
175  def set_xlim(self, limits):
176  self._canvas.axes.set_xbound(lower=limits[0], upper=limits[1])
177 
178  def set_ylim(self, limits):
179  self._canvas.axes.set_ybound(lower=limits[0], upper=limits[1])
180 
181  def get_xlim(self):
182  return list(self._canvas.axes.get_xbound())
183 
184  def get_ylim(self):
185  return list(self._canvas.axes.get_ybound())
def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False)
def set_values(self, curve, data_x, data_y)


rqt_plot
Author(s): Dorian Scholz
autogenerated on Sun Mar 17 2019 02:21:35