pyqtgraph_data_plot.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2011, Dorian Scholz
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #   * Redistributions of source code must retain the above copyright
00011 #     notice, this list of conditions and the following disclaimer.
00012 #   * Redistributions in binary form must reproduce the above
00013 #     copyright notice, this list of conditions and the following
00014 #     disclaimer in the documentation and/or other materials provided
00015 #     with the distribution.
00016 #   * Neither the name of the TU Darmstadt nor the names of its
00017 #     contributors may be used to endorse or promote products derived
00018 #     from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from python_qt_binding.QtCore import Slot, Qt, qVersion, qWarning, Signal
00034 from python_qt_binding.QtGui import QColor
00035 from python_qt_binding.QtWidgets import QVBoxLayout, QWidget
00036 
00037 if qVersion().startswith('5.'):
00038     try:
00039         from pkg_resources import parse_version
00040     except:
00041         import re
00042 
00043         def parse_version(s):
00044             return [int(x) for x in re.sub(r'(\.0+)*$', '', s).split('.')]
00045 
00046     try:
00047         from pyqtgraph import __version__ as pyqtgraph_version
00048     except RuntimeError:
00049         # pyqtgraph < 1.0 using Qt4 failing on 16.04 because kinetic uses Qt5.
00050         # This raises RuntimeError('the PyQt4.QtCore and PyQt5.QtCore modules both
00051         # wrap the QObject class')
00052         import pkg_resources
00053         pyqtgraph_version = pkg_resources.get_distribution("pyqtgraph").version
00054 
00055     if parse_version(pyqtgraph_version) < parse_version('0.10.0'):
00056         raise ImportError('A newer PyQtGraph version is required (at least 0.10 for Qt 5)')
00057 
00058 from pyqtgraph import PlotWidget, mkPen, mkBrush
00059 import numpy
00060 
00061 
00062 class PyQtGraphDataPlot(QWidget):
00063 
00064     limits_changed = Signal()
00065 
00066     def __init__(self, parent=None):
00067         super(PyQtGraphDataPlot, self).__init__(parent)
00068         self._plot_widget = PlotWidget()
00069         self._plot_widget.getPlotItem().addLegend()
00070         self._plot_widget.setBackground((255, 255, 255))
00071         self._plot_widget.setXRange(0, 10, padding=0)
00072         vbox = QVBoxLayout()
00073         vbox.addWidget(self._plot_widget)
00074         self.setLayout(vbox)
00075         self._plot_widget.getPlotItem().sigRangeChanged.connect(self.limits_changed)
00076 
00077         self._curves = {}
00078         self._current_vline = None
00079 
00080     def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False):
00081         pen = mkPen(curve_color, width=1)
00082         symbol = "o"
00083         symbolPen = mkPen(QColor(Qt.black))
00084         symbolBrush = mkBrush(curve_color)
00085         # this adds the item to the plot and legend
00086         if markers_on:
00087             plot = self._plot_widget.plot(name=curve_name, pen=pen, symbol=symbol,
00088                                           symbolPen=symbolPen, symbolBrush=symbolBrush, symbolSize=4)
00089         else:
00090             plot = self._plot_widget.plot(name=curve_name, pen=pen)
00091         self._curves[curve_id] = plot
00092 
00093     def remove_curve(self, curve_id):
00094         curve_id = str(curve_id)
00095         if curve_id in self._curves:
00096             self._plot_widget.removeItem(self._curves[curve_id])
00097             del self._curves[curve_id]
00098             self._update_legend()
00099 
00100     def _update_legend(self):
00101         # clear and rebuild legend (there is no remove item method for the legend...)
00102         self._plot_widget.clear()
00103         self._plot_widget.getPlotItem().legend.items = []
00104         for curve in self._curves.values():
00105             self._plot_widget.addItem(curve)
00106         if self._current_vline:
00107             self._plot_widget.addItem(self._current_vline)
00108 
00109     def redraw(self):
00110         pass
00111 
00112     def set_values(self, curve_id, data_x, data_y):
00113         curve = self._curves[curve_id]
00114         curve.setData(data_x, data_y)
00115 
00116     def vline(self, x, color):
00117         if self._current_vline:
00118             self._plot_widget.removeItem(self._current_vline)
00119         self._current_vline = self._plot_widget.addLine(x=x, pen=color)
00120 
00121     def set_xlim(self, limits):
00122         # TODO: this doesn't seem to handle fast updates well
00123         self._plot_widget.setXRange(limits[0], limits[1], padding=0)
00124 
00125     def set_ylim(self, limits):
00126         self._plot_widget.setYRange(limits[0], limits[1], padding=0)
00127 
00128     def get_xlim(self):
00129         x_range, _ = self._plot_widget.viewRange()
00130         return x_range
00131 
00132     def get_ylim(self):
00133         _, y_range = self._plot_widget.viewRange()
00134         return y_range


rqt_plot
Author(s): Dorian Scholz
autogenerated on Sun Mar 17 2019 02:29:34