pyqtgraph_data_plot.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2011, 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.QtCore import Slot, Qt, qVersion, qWarning, Signal
34 from python_qt_binding.QtGui import QColor
35 from python_qt_binding.QtWidgets import QVBoxLayout, QWidget
36 
37 if qVersion().startswith('5.'):
38  try:
39  from pkg_resources import parse_version
40  except:
41  import re
42 
43  def parse_version(s):
44  return [int(x) for x in re.sub(r'(\.0+)*$', '', s).split('.')]
45 
46  try:
47  from pyqtgraph import __version__ as pyqtgraph_version
48  except RuntimeError:
49  # pyqtgraph < 1.0 using Qt4 failing on 16.04 because kinetic uses Qt5.
50  # This raises RuntimeError('the PyQt4.QtCore and PyQt5.QtCore modules both
51  # wrap the QObject class')
52  import pkg_resources
53  pyqtgraph_version = pkg_resources.get_distribution("pyqtgraph").version
54 
55  if parse_version(pyqtgraph_version) < parse_version('0.10.0'):
56  raise ImportError('A newer PyQtGraph version is required (at least 0.10 for Qt 5)')
57 
58 from pyqtgraph import PlotWidget, mkPen, mkBrush
59 import numpy
60 
61 
62 class PyQtGraphDataPlot(QWidget):
63 
64  limits_changed = Signal()
65 
66  def __init__(self, parent=None):
67  super(PyQtGraphDataPlot, self).__init__(parent)
68  self._plot_widget = PlotWidget()
69  self._plot_widget.getPlotItem().addLegend()
70  self._plot_widget.setBackground((255, 255, 255))
71  self._plot_widget.setXRange(0, 10, padding=0)
72  vbox = QVBoxLayout()
73  vbox.addWidget(self._plot_widget)
74  self.setLayout(vbox)
75  self._plot_widget.getPlotItem().sigRangeChanged.connect(self.limits_changed)
76 
77  self._curves = {}
78  self._current_vline = None
79 
80  def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False):
81  pen = mkPen(curve_color, width=1)
82  symbol = "o"
83  symbolPen = mkPen(QColor(Qt.black))
84  symbolBrush = mkBrush(curve_color)
85  # this adds the item to the plot and legend
86  if markers_on:
87  plot = self._plot_widget.plot(name=curve_name, pen=pen, symbol=symbol,
88  symbolPen=symbolPen, symbolBrush=symbolBrush, symbolSize=4)
89  else:
90  plot = self._plot_widget.plot(name=curve_name, pen=pen)
91  self._curves[curve_id] = plot
92 
93  def remove_curve(self, curve_id):
94  curve_id = str(curve_id)
95  if curve_id in self._curves:
96  self._plot_widget.removeItem(self._curves[curve_id])
97  del self._curves[curve_id]
98  self._update_legend()
99 
100  def _update_legend(self):
101  # clear and rebuild legend (there is no remove item method for the legend...)
102  self._plot_widget.clear()
103  self._plot_widget.getPlotItem().legend.items = []
104  for curve in self._curves.values():
105  self._plot_widget.addItem(curve)
106  if self._current_vline:
107  self._plot_widget.addItem(self._current_vline)
108 
109  def redraw(self):
110  pass
111 
112  def set_values(self, curve_id, data_x, data_y):
113  curve = self._curves[curve_id]
114  curve.setData(data_x, data_y)
115 
116  def vline(self, x, color):
117  if self._current_vline:
118  self._plot_widget.removeItem(self._current_vline)
119  self._current_vline = self._plot_widget.addLine(x=x, pen=color)
120 
121  def set_xlim(self, limits):
122  # TODO: this doesn't seem to handle fast updates well
123  self._plot_widget.setXRange(limits[0], limits[1], padding=0)
124 
125  def set_ylim(self, limits):
126  self._plot_widget.setYRange(limits[0], limits[1], padding=0)
127 
128  def get_xlim(self):
129  x_range, _ = self._plot_widget.viewRange()
130  return x_range
131 
132  def get_ylim(self):
133  _, y_range = self._plot_widget.viewRange()
134  return y_range
def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False)


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