本文整理汇总了Python中pyqtgraph.PlotWidget.removeItem方法的典型用法代码示例。如果您正苦于以下问题:Python PlotWidget.removeItem方法的具体用法?Python PlotWidget.removeItem怎么用?Python PlotWidget.removeItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyqtgraph.PlotWidget
的用法示例。
在下文中一共展示了PlotWidget.removeItem方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RealtimePlotWidget
# 需要导入模块: from pyqtgraph import PlotWidget [as 别名]
# 或者: from pyqtgraph.PlotWidget import removeItem [as 别名]
class RealtimePlotWidget(QWidget):
COLORS = [Qt.red, Qt.blue, Qt.green, Qt.magenta, Qt.cyan,
Qt.darkRed, Qt.darkBlue, Qt.darkGreen, Qt.darkYellow, Qt.gray]
def __init__(self, parent=None):
super(RealtimePlotWidget, self).__init__(parent)
self._plot_widget = PlotWidget()
self._plot_widget.setBackground((0, 0, 0))
self._plot_widget.addLegend()
self._plot_widget.showButtons()
self._plot_widget.enableAutoRange()
self._plot_widget.showGrid(x=True, y=True, alpha=0.2)
vbox = QVBoxLayout()
vbox.addWidget(self._plot_widget)
self.setLayout(vbox)
self._color_index = 0
self._curves = {}
def add_curve(self, curve_id, curve_name, data_x=[], data_y=[]):
color = QColor(self.COLORS[self._color_index % len(self.COLORS)])
self._color_index += 1
pen = mkPen(color, width=1)
plot = self._plot_widget.plot(name=curve_name, pen=pen)
data_x = numpy.array(data_x)
data_y = numpy.array(data_y)
self._curves[curve_id] = {'x': data_x, 'y': data_y, 'plot': plot}
def remove_curve(self, curve_id):
curve_id = str(curve_id)
if curve_id in self._curves:
self._plot_widget.removeItem(self._curves[curve_id]['plot'])
del self._curves[curve_id]
def set_x_range(self, left, right):
self._plot_widget.setRange(xRange=(left, right))
def update_values(self, curve_id, x, y):
curve = self._curves[curve_id]
curve['x'] = numpy.append(curve['x'], x)
curve['y'] = numpy.append(curve['y'], y)
def redraw(self):
for curve in self._curves.values():
if len(curve['x']):
curve['plot'].setData(curve['x'], curve['y'])
def lazy_redraw(self, period):
timestamp = time.time()
if not hasattr(self, '_prev_lazy_redraw'):
self._prev_lazy_redraw = 0.0
if timestamp - self._prev_lazy_redraw > period:
self._prev_lazy_redraw = timestamp
self.redraw()
示例2: PyQtGraphDataPlot
# 需要导入模块: from pyqtgraph import PlotWidget [as 别名]
# 或者: from pyqtgraph.PlotWidget import removeItem [as 别名]
class PyQtGraphDataPlot(QWidget):
limits_changed = Signal()
def __init__(self, parent=None):
super(PyQtGraphDataPlot, self).__init__(parent)
self._plot_widget = PlotWidget()
self._plot_widget.getPlotItem().addLegend()
self._plot_widget.setBackground((255, 255, 255))
self._plot_widget.setXRange(0, 10, padding=0)
vbox = QVBoxLayout()
vbox.addWidget(self._plot_widget)
self.setLayout(vbox)
self._plot_widget.getPlotItem().sigRangeChanged.connect(self.limits_changed)
self._curves = {}
self._current_vline = None
def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False):
pen = mkPen(curve_color, width=1)
symbol = "o"
symbolPen = mkPen(QColor(Qt.black))
symbolBrush = mkBrush(curve_color)
# this adds the item to the plot and legend
if markers_on:
plot = self._plot_widget.plot(name=curve_name, pen=pen, symbol=symbol, symbolPen=symbolPen, symbolBrush=symbolBrush, symbolSize=4)
else:
plot = self._plot_widget.plot(name=curve_name, pen=pen)
self._curves[curve_id] = plot
def remove_curve(self, curve_id):
curve_id = str(curve_id)
if curve_id in self._curves:
self._plot_widget.removeItem(self._curves[curve_id])
del self._curves[curve_id]
self._update_legend()
def _update_legend(self):
# clear and rebuild legend (there is no remove item method for the legend...)
self._plot_widget.clear()
self._plot_widget.getPlotItem().legend.items = []
for curve in self._curves.values():
self._plot_widget.addItem(curve)
if self._current_vline:
self._plot_widget.addItem(self._current_vline)
def redraw(self):
pass
def set_values(self, curve_id, data_x, data_y):
curve = self._curves[curve_id]
curve.setData(data_x, data_y)
def vline(self, x, color):
if self._current_vline:
self._plot_widget.removeItem(self._current_vline)
self._current_vline = self._plot_widget.addLine(x=x, pen=color)
def set_xlim(self, limits):
# TODO: this doesn't seem to handle fast updates well
self._plot_widget.setXRange(limits[0], limits[1], padding=0)
def set_ylim(self, limits):
self._plot_widget.setYRange(limits[0], limits[1], padding=0)
def get_xlim(self):
x_range, _ = self._plot_widget.viewRange()
return x_range
def get_ylim(self):
_, y_range = self._plot_widget.viewRange()
return y_range
示例3: Plotter
# 需要导入模块: from pyqtgraph import PlotWidget [as 别名]
# 或者: from pyqtgraph.PlotWidget import removeItem [as 别名]
#.........这里部分代码省略.........
self._autoscroll.setChecked(True)
self._max_x = self.INITIAL_X_RANGE
# Layout
control_panel = QHBoxLayout()
control_panel.addWidget(button_add_matcher)
control_panel.addWidget(button_clear_plots)
control_panel.addWidget(self._autoscroll)
control_panel.addStretch()
control_panel.addWidget(button_delete_all_matchers)
self._curve_matcher_container = QVBoxLayout()
layout = QVBoxLayout()
layout.addWidget(self._plot_widget, 1)
layout.addLayout(control_panel)
layout.addLayout(self._curve_matcher_container)
self.setLayout(layout)
# Logic
self._color_index = 0
self._curves = {}
self._message_queue = multiprocessing.Queue()
self._active_messages = set() # set(data type name)
self._curve_matchers = []
# Defaults
self._add_curve_matcher(CurveMatcher('uavcan.protocol.debug.KeyValue', 'value', [('key', None)]))
def _add_curve_matcher(self, matcher):
self._curve_matchers.append(matcher)
view = CurveMatcherView(matcher, self)
def remove():
self._curve_matchers.remove(matcher)
self._curve_matcher_container.removeWidget(view)
view.setParent(None)
view.deleteLater()
view.on_remove = remove
self._curve_matcher_container.addWidget(view)
def _update(self):
# Processing messages
while True:
try:
m = self._message_queue.get_nowait()
self._process_message(m)
except queue.Empty:
break
# Updating curves
for curve in self._curves.values():
if len(curve['x']):
if len(curve['x']) > self.MAX_DATA_POINTS_PER_CURVE:
curve['x'] = curve['x'][-self.MAX_DATA_POINTS_PER_CURVE:]
curve['y'] = curve['y'][-self.MAX_DATA_POINTS_PER_CURVE:]
assert len(curve['x']) == len(curve['y'])
curve['plot'].setData(curve['x'], curve['y'])
self._max_x = max(self._max_x, curve['x'][-1])
# Updating view range
if self._autoscroll.checkState():
(xmin, xmax), _ = self._plot_widget.viewRange()
diff = xmax - xmin
xmax = self._max_x
xmin = self._max_x - diff
self._plot_widget.setRange(xRange=(xmin, xmax), padding=0)
def _process_message(self, m):
self._active_messages.add(m.data_type_name)
for matcher in self._curve_matchers:
if matcher.match(m):
name, x, y = matcher.extract_curve_name_x_y(m)
self._draw_curve(name, x, y)
def _remove_all_curves(self):
for curve in self._curves.values():
self._plot_widget.removeItem(curve['plot'])
self._plot_widget.clear()
self._curves = {}
self._color_index = 0
self._legend.scene().removeItem(self._legend)
self._legend = self._plot_widget.addLegend()
def _draw_curve(self, name, x, y):
if name not in self._curves:
logging.info('Adding curve %r', name)
color = self.COLORS[self._color_index % len(self.COLORS)]
self._color_index += 1
pen = mkPen(QColor(color), width=1)
plot = self._plot_widget.plot(name=name, pen=pen)
self._curves[name] = {'x': numpy.array([]), 'y': numpy.array([]), 'plot': plot}
curve = self._curves[name]
curve['x'] = numpy.append(curve['x'], [x] if isinstance(x, (float, int)) else x)
curve['y'] = numpy.append(curve['y'], [y] if isinstance(y, (float, int)) else y)
assert len(curve['x']) == len(curve['y'])
def push_received_message(self, msg):
self._message_queue.put_nowait(msg)
示例4: RealtimePlotWidget
# 需要导入模块: from pyqtgraph import PlotWidget [as 别名]
# 或者: from pyqtgraph.PlotWidget import removeItem [as 别名]
class RealtimePlotWidget(QWidget):
AUTO_RANGE_FRACTION = 0.99
COLORS = [Qt.red, Qt.blue, Qt.green, Qt.magenta, Qt.cyan,
Qt.darkRed, Qt.darkBlue, Qt.darkGreen, Qt.darkYellow, Qt.gray]
def __init__(self, display_measurements, parent):
super(RealtimePlotWidget, self).__init__(parent)
self.setAttribute(Qt.WA_DeleteOnClose) # This is required to stop background timers!
self._plot_widget = PlotWidget()
self._plot_widget.setBackground((0, 0, 0))
self._legend = self._plot_widget.addLegend()
self._plot_widget.showButtons()
self._plot_widget.showGrid(x=True, y=True, alpha=0.3)
vbox = QVBoxLayout(self)
vbox.addWidget(self._plot_widget)
self.setLayout(vbox)
self._last_update_ts = 0
self._reset_required = False
self._update_timer = QTimer(self)
self._update_timer.setSingleShot(False)
self._update_timer.timeout.connect(self._update)
self._update_timer.start(200)
self._color_index = 0
self._curves = {}
# Crosshair
def _render_measurements(cur, ref):
text = 'time %.6f sec, y %.6f' % cur
if ref is None:
return text
dt = cur[0] - ref[0]
dy = cur[1] - ref[1]
if abs(dt) > 1e-12:
freq = '%.6f' % abs(1 / dt)
else:
freq = 'inf'
display_measurements(text + ';' + ' ' * 4 + 'dt %.6f sec, freq %s Hz, dy %.6f' % (dt, freq, dy))
display_measurements('Hover to sample Time/Y, click to set new reference')
add_crosshair(self._plot_widget, _render_measurements)
# Final reset
self.reset()
def _trigger_auto_reset_if_needed(self):
ts = time.monotonic()
dt = ts - self._last_update_ts
self._last_update_ts = ts
if dt > 2:
self._reset_required = True
def add_curve(self, curve_id, curve_name, data_x=[], data_y=[]):
color = QColor(self.COLORS[self._color_index % len(self.COLORS)])
self._color_index += 1
pen = mkPen(color, width=1)
plot = self._plot_widget.plot(name=curve_name, pen=pen)
data_x = numpy.array(data_x)
data_y = numpy.array(data_y)
self._curves[curve_id] = {'data': (data_x, data_y), 'plot': plot}
self._trigger_auto_reset_if_needed()
def update_values(self, curve_id, x, y):
curve = self._curves[curve_id]
old_x, old_y = curve['data']
curve['data'] = numpy.append(old_x, x), numpy.append(old_y, y)
self._trigger_auto_reset_if_needed()
def reset(self):
for curve in self._curves.keys():
self._plot_widget.removeItem(self._curves[curve]['plot'])
self._curves = {}
self._color_index = 0
self._plot_widget.enableAutoRange(enable=self.AUTO_RANGE_FRACTION,
x=self.AUTO_RANGE_FRACTION,
y=self.AUTO_RANGE_FRACTION)
self._legend.scene().removeItem(self._legend)
self._legend = self._plot_widget.addLegend()
def _update(self):
if self._reset_required:
self.reset()
self._reset_required = False
for curve in self._curves.values():
if len(curve['data'][0]):
curve['plot'].setData(*curve['data'])