mat_data_plot.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2011, Ye Cheng, 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 import QT_BINDING, QT_BINDING_VERSION
00034 if QT_BINDING == 'pyside':
00035     try:
00036         from pkg_resources import parse_version
00037     except:
00038         import re
00039 
00040         def parse_version(s):
00041             return [int(x) for x in re.sub(r'(\.0+)*$', '', s).split('.')]
00042     if parse_version(QT_BINDING_VERSION) <= parse_version('1.1.2'):
00043         raise ImportError('A PySide version newer than 1.1.0 is required.')
00044 
00045 from python_qt_binding.QtCore import Slot, Qt, qWarning, Signal
00046 from python_qt_binding.QtGui import QWidget, QVBoxLayout, QSizePolicy, QColor
00047 
00048 import operator
00049 import matplotlib
00050 if matplotlib.__version__ < '1.1.0':
00051     raise ImportError('A newer matplotlib is required (at least 1.1.0)')
00052 
00053 try:
00054     from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
00055 except ImportError:
00056     # work around bug in dateutil
00057     import sys
00058     import thread
00059     sys.modules['_thread'] = thread
00060     from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
00061 from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
00062 from matplotlib.figure import Figure
00063 
00064 import numpy
00065 
00066 
00067 class MatDataPlot(QWidget):
00068     class Canvas(FigureCanvas):
00069         """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
00070         def __init__(self, parent=None):
00071             super(MatDataPlot.Canvas, self).__init__(Figure())
00072             self.axes = self.figure.add_subplot(111)
00073             self.axes.grid(True, color='gray')
00074             self.figure.tight_layout()
00075             self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
00076             self.updateGeometry()
00077 
00078         def resizeEvent(self, event):
00079             super(MatDataPlot.Canvas, self).resizeEvent(event)
00080             self.figure.tight_layout()
00081 
00082     limits_changed = Signal()
00083 
00084     def __init__(self, parent=None):
00085         super(MatDataPlot, self).__init__(parent)
00086         self._canvas = MatDataPlot.Canvas()
00087         self._toolbar = NavigationToolbar(self._canvas, self._canvas)
00088         vbox = QVBoxLayout()
00089         vbox.addWidget(self._toolbar)
00090         vbox.addWidget(self._canvas)
00091         self.setLayout(vbox)
00092 
00093         self._curves = {}
00094         self._current_vline = None
00095         self._canvas.mpl_connect('button_release_event', self._limits_changed)
00096 
00097     def _limits_changed(self, event):
00098         self.limits_changed.emit()
00099 
00100     def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False):
00101 
00102         # adding an empty curve and change the limits, so save and restore them
00103         x_limits = self.get_xlim()
00104         y_limits = self.get_ylim()
00105         if markers_on:
00106             marker_size = 3
00107         else:
00108             marker_size = 0
00109         line = self._canvas.axes.plot([], [], 'o-', markersize=marker_size, label=curve_name, linewidth=1, picker=5, color=curve_color.name())[0]
00110         self._curves[curve_id] = line
00111         self._update_legend()
00112         self.set_xlim(x_limits)
00113         self.set_ylim(y_limits)
00114 
00115     def remove_curve(self, curve_id):
00116         curve_id = str(curve_id)
00117         if curve_id in self._curves:
00118             self._curves[curve_id].remove()
00119             del self._curves[curve_id]
00120             self._update_legend()
00121 
00122     def _update_legend(self):
00123         handles, labels = self._canvas.axes.get_legend_handles_labels()
00124         if handles:
00125             hl = sorted(zip(handles, labels), key=operator.itemgetter(1))
00126             handles, labels = zip(*hl)
00127         self._canvas.axes.legend(handles, labels, loc='upper left')
00128 
00129     def set_values(self, curve, data_x, data_y):
00130         line = self._curves[curve]
00131         line.set_data(data_x, data_y)
00132 
00133     def redraw(self):
00134         self._canvas.axes.grid(True, color='gray')
00135         self._canvas.draw()
00136 
00137     def vline(self, x, color):
00138         # convert color range from (0,255) to (0,1.0) 
00139         matcolor=(color[0]/255.0, color[1]/255.0, color[2]/255.0)
00140         if self._current_vline:
00141             self._current_vline.remove()
00142         self._current_vline = self._canvas.axes.axvline(x=x, color=matcolor)
00143 
00144     def set_xlim(self, limits):
00145         self._canvas.axes.set_xbound(lower=limits[0], upper=limits[1])
00146 
00147     def set_ylim(self, limits):
00148         self._canvas.axes.set_ybound(lower=limits[0], upper=limits[1])
00149 
00150     def get_xlim(self):
00151         return list(self._canvas.axes.get_xbound())
00152 
00153     def get_ylim(self):
00154         return list(self._canvas.axes.get_ybound())


rqt_plot
Author(s): Dorian Scholz
autogenerated on Mon Oct 6 2014 07:15:32