Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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 from pyqtgraph import __version__ as pyqtgraph_version
00047 if parse_version(pyqtgraph_version) < parse_version('0.10.0'):
00048 raise ImportError('A newer PyQtGraph version is required (at least 0.10 for Qt 5)')
00049
00050 from pyqtgraph import PlotWidget, mkPen, mkBrush
00051 import numpy
00052
00053
00054 class PyQtGraphDataPlot(QWidget):
00055
00056 limits_changed = Signal()
00057
00058 def __init__(self, parent=None):
00059 super(PyQtGraphDataPlot, self).__init__(parent)
00060 self._plot_widget = PlotWidget()
00061 self._plot_widget.getPlotItem().addLegend()
00062 self._plot_widget.setBackground((255, 255, 255))
00063 self._plot_widget.setXRange(0, 10, padding=0)
00064 vbox = QVBoxLayout()
00065 vbox.addWidget(self._plot_widget)
00066 self.setLayout(vbox)
00067 self._plot_widget.getPlotItem().sigRangeChanged.connect(self.limits_changed)
00068
00069 self._curves = {}
00070 self._current_vline = None
00071
00072 def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False):
00073 pen = mkPen(curve_color, width=1)
00074 symbol = "o"
00075 symbolPen = mkPen(QColor(Qt.black))
00076 symbolBrush = mkBrush(curve_color)
00077
00078 if markers_on:
00079 plot = self._plot_widget.plot(name=curve_name, pen=pen, symbol=symbol, symbolPen=symbolPen, symbolBrush=symbolBrush, symbolSize=4)
00080 else:
00081 plot = self._plot_widget.plot(name=curve_name, pen=pen)
00082 self._curves[curve_id] = plot
00083
00084 def remove_curve(self, curve_id):
00085 curve_id = str(curve_id)
00086 if curve_id in self._curves:
00087 self._plot_widget.removeItem(self._curves[curve_id])
00088 del self._curves[curve_id]
00089 self._update_legend()
00090
00091 def _update_legend(self):
00092
00093 self._plot_widget.clear()
00094 self._plot_widget.getPlotItem().legend.items = []
00095 for curve in self._curves.values():
00096 self._plot_widget.addItem(curve)
00097 if self._current_vline:
00098 self._plot_widget.addItem(self._current_vline)
00099
00100 def redraw(self):
00101 pass
00102
00103 def set_values(self, curve_id, data_x, data_y):
00104 curve = self._curves[curve_id]
00105 curve.setData(data_x, data_y)
00106
00107 def vline(self, x, color):
00108 if self._current_vline:
00109 self._plot_widget.removeItem(self._current_vline)
00110 self._current_vline = self._plot_widget.addLine(x=x, pen=color)
00111
00112 def set_xlim(self, limits):
00113
00114 self._plot_widget.setXRange(limits[0], limits[1], padding=0)
00115
00116 def set_ylim(self, limits):
00117 self._plot_widget.setYRange(limits[0], limits[1], padding=0)
00118
00119 def get_xlim(self):
00120 x_range, _ = self._plot_widget.viewRange()
00121 return x_range
00122
00123 def get_ylim(self):
00124 _, y_range = self._plot_widget.viewRange()
00125 return y_range