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
00034 from python_qt_binding.QtGui import QColor, QVBoxLayout, QWidget
00035 
00036 from pyqtgraph import PlotWidget, mkPen
00037 import numpy
00038 
00039 
00040 class PyQtGraphDataPlot(QWidget):
00041     _colors = [Qt.red, Qt.blue, Qt.magenta, Qt.cyan, Qt.green, Qt.darkYellow, Qt.black, Qt.darkRed, Qt.gray, Qt.darkCyan]
00042 
00043     def __init__(self, parent=None):
00044         super(PyQtGraphDataPlot, self).__init__(parent)
00045         self._plot_widget = PlotWidget()
00046         self._plot_widget.setBackground((255, 255, 255))
00047         self._plot_widget.setXRange(0, 10, padding=0)
00048         vbox = QVBoxLayout()
00049         vbox.addWidget(self._plot_widget)
00050         self.setLayout(vbox)
00051 
00052         self._color_index = 0
00053         self._curves = {}
00054 
00055     def add_curve(self, curve_id, curve_name, data_x, data_y):
00056         color = QColor(self._colors[self._color_index % len(self._colors)])
00057         self._color_index += 1
00058         pen = mkPen(color, width=1)
00059         plot = self._plot_widget.plot(name=curve_name, pen=pen)
00060         data_x = numpy.array(data_x)
00061         data_y = numpy.array(data_y)
00062         self._curves[curve_id] = {'x': data_x, 'y': data_y, 'plot': plot}
00063         self._update_legend()
00064 
00065     def remove_curve(self, curve_id):
00066         curve_id = str(curve_id)
00067         if curve_id in self._curves:
00068             self._plot_widget.removeItem(self._curves[curve_id]['plot'])
00069             del self._curves[curve_id]
00070             self._update_legend()
00071 
00072     def _update_legend(self):
00073         pass
00074 
00075     @Slot(str, list, list)
00076     def update_values(self, curve_id, x, y):
00077         curve = self._curves[curve_id]
00078         curve['x'] = numpy.append(curve['x'], x)
00079         curve['y'] = numpy.append(curve['y'], y)
00080 
00081     def redraw(self):
00082         # Set axis bounds
00083         x_range, _ = self._plot_widget.viewRange()
00084         x_delta = x_range[1] - x_range[0]
00085         x_max = 0
00086         for curve in self._curves.values():
00087             if len(curve['x']) == 0:
00088                 continue
00089 
00090             x_max = max(x_max, curve['x'][-1])
00091             curve['plot'].setData(curve['x'], curve['y'])
00092 
00093         self._plot_widget.setXRange(x_max - x_delta, x_max, padding=0)


rqt_plot
Author(s): Dorian Scholz
autogenerated on Fri Jan 3 2014 11:55:13