我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用pyqtgraph.ScatterPlotItem()。
def test_init_spots(): plot = pg.PlotWidget() # set view range equal to its bounding rect. # This causes plots to look the same regardless of pxMode. plot.setRange(rect=plot.boundingRect()) spots = [ {'x': 0, 'y': 1}, {'pos': (1, 2), 'pen': None, 'brush': None, 'data': 'zzz'}, ] s = pg.ScatterPlotItem(spots=spots) # Check we can display without errors plot.addItem(s) app.processEvents() plot.clear() # check data is correct spots = s.points() defPen = pg.mkPen(pg.getConfigOption('foreground')) assert spots[0].pos().x() == 0 assert spots[0].pos().y() == 1 assert spots[0].pen() == defPen assert spots[0].data() is None assert spots[1].pos().x() == 1 assert spots[1].pos().y() == 2 assert spots[1].pen() == pg.mkPen(None) assert spots[1].brush() == pg.mkBrush(None) assert spots[1].data() == 'zzz'
def plot_scatter_points(self, x_data, y_data): if self.scatterplot_item: self.plotview.removeItem(self.scatterplot_item) self.n_points = len(x_data) self.scatterplot_item = pg.ScatterPlotItem( x=x_data, y=y_data, data=np.arange(self.n_points), symbol="o", size=10, pen=pg.mkPen(0.2), brush=pg.mkBrush(0.7), antialias=True) self.scatterplot_item.opts["useCache"] = False self.plotview.addItem(self.scatterplot_item) self.plotview.replot()
def from_layer(layer, **kwargs): """Create a LinePlot from a layer Parameters ---------- layer: `Spectrum1DRefLayer` The layer to create from. kwargs: dict Other arguments for `LinePlot` class. Returns ------- plot_container: The new LinePlot """ plot_data_item = pg.PlotDataItem(layer.masked_dispersion, layer.masked_data) plot_container = LinePlot(layer=layer, plot=plot_data_item, **kwargs) if plot_container.layer.raw_uncertainty is not None: plot_error_item = pg.ErrorBarItem( x=plot_container.layer.masked_dispersion.compressed().value, y=plot_container.layer.masked_data.compressed().value, height=plot_container.layer.raw_uncertainty.compressed().value, ) plot_container.error = plot_error_item if plot_container.layer.mask is not None: mask = plot_container.layer.mask x = plot_container.layer.masked_dispersion.data.value[mask] y = plot_container.layer.masked_data.data.value[mask] plot_mask_item = pg.ScatterPlotItem( x=x, y=y, symbol='x' ) plot_container.mask = plot_mask_item return plot_container
def __init__(self, graph): ViewBox.__init__(self, enableMenu=False) self.graph = graph self.setMouseMode(self.PanMode) self.zoomstartpoint = None self.current_selection = None self.action = PANNING self.y_padding = 0.02 self.x_padding = 0 # line for marking selection self.selection_line = pg.PlotCurveItem() self.selection_line.setPen(pg.mkPen(color=QColor(Qt.black), width=2, style=Qt.DotLine)) self.selection_line.setZValue(1e9) self.selection_line.hide() self.addItem(self.selection_line, ignoreBounds=True) # yellow marker for ending the polygon self.selection_poly_marker = pg.ScatterPlotItem() self.selection_poly_marker.setPen(pg.mkPen(color=QColor(Qt.yellow), width=2)) self.selection_poly_marker.setSize(SELECT_POLYGON_TOLERANCE*2) self.selection_poly_marker.setBrush(None) self.selection_poly_marker.setZValue(1e9+1) self.selection_poly_marker.hide() self.selection_poly_marker.mouseClickEvent = lambda x: x # ignore mouse clicks self.addItem(self.selection_poly_marker, ignoreBounds=True)
def test_scatterplotitem(): plot = pg.PlotWidget() # set view range equal to its bounding rect. # This causes plots to look the same regardless of pxMode. plot.setRange(rect=plot.boundingRect()) for i, pxMode in enumerate([True, False]): for j, useCache in enumerate([True, False]): s = pg.ScatterPlotItem() s.opts['useCache'] = useCache plot.addItem(s) s.setData(x=np.array([10,40,20,30])+i*100, y=np.array([40,60,10,30])+j*100, pxMode=pxMode) s.addPoints(x=np.array([60, 70])+i*100, y=np.array([60, 70])+j*100, size=[20, 30]) # Test uniform spot updates s.setSize(10) s.setBrush('r') s.setPen('g') s.setSymbol('+') app.processEvents() # Test list spot updates s.setSize([10] * 6) s.setBrush([pg.mkBrush('r')] * 6) s.setPen([pg.mkPen('g')] * 6) s.setSymbol(['+'] * 6) s.setPointData([s] * 6) app.processEvents() # Test array spot updates s.setSize(np.array([10] * 6)) s.setBrush(np.array([pg.mkBrush('r')] * 6)) s.setPen(np.array([pg.mkPen('g')] * 6)) s.setSymbol(np.array(['+'] * 6)) s.setPointData(np.array([s] * 6)) app.processEvents() # Test per-spot updates spot = s.points()[0] spot.setSize(20) spot.setBrush('b') spot.setPen('g') spot.setSymbol('o') spot.setData(None) app.processEvents() plot.clear()
def plot_trace(xy, ids = None, depth = 0, colormap = 'rainbow', line_color = 'k', line_width = 1, point_size = 5, title = None): """Plot trajectories with positions color coded according to discrete ids""" #if ids is not None: uids = np.unique(ids); cmap = cm.get_cmap(colormap); n = len(uids); colors = cmap(range(n), bytes = True); #lines if line_width is not None: #plt.plot(xy[:,0], xy[:,1], color = lines); plot = pg.plot(xy[:,0], xy[:,1], pen = pg.mkPen(color = line_color, width = line_width)) else: plot = pg.plot(title = title); if ids is None: sp = pg.ScatterPlotItem(pos = xy, size=point_size, pen=pg.mkPen(colors[0])); #, pxMode=True); else: sp = pg.ScatterPlotItem(size=point_size); #, pxMode=True); spots = []; for j,i in enumerate(uids): idx = ids == i; spots.append({'pos': xy[idx,:].T, 'data': 1, 'brush':pg.mkBrush(colors[j])}); #, 'size': point_size}); sp.addPoints(spots) plot.addItem(sp); return plot; # legs = []; # for k,i in enumerate(uids): # ii = np.where(ids == i)[0]; # if depth > 0: # ii = [ii-d for d in range(depth)]; # ii = np.unique(np.concatenate(ii)); # # plt.plot(data[ii, 0], data[ii, 1], '.', color = color[k]); # # legs.append(mpatches.Patch(color=color[k], label= str(i))); # # plt.legend(handles=legs);
def refresh_integral_markings(dis, markings_list, curveplot): for m in markings_list: if m in curveplot.markings: curveplot.remove_marking(m) markings_list.clear() def add_marking(a): markings_list.append(a) curveplot.add_marking(a) for di in dis: if di is None: continue # nothing to draw color = QColor(di.get("color", "red")) for el in di["draw"]: if el[0] == "curve": bs_x, bs_ys, penargs = el[1] curve = pg.PlotCurveItem() curve.setPen(pg.mkPen(color=QColor(color), **penargs)) curve.setZValue(10) curve.setData(x=bs_x, y=bs_ys[0]) add_marking(curve) elif el[0] == "fill": (x1, ys1), (x2, ys2) = el[1] phigh = pg.PlotCurveItem(x1, ys1[0], pen=None) plow = pg.PlotCurveItem(x2, ys2[0], pen=None) color = QColor(color) color.setAlphaF(0.5) cc = pg.mkBrush(color) pfill = pg.FillBetweenItem(plow, phigh, brush=cc) pfill.setZValue(9) add_marking(pfill) elif el[0] == "line": (x1, y1), (x2, y2) = el[1] line = pg.PlotCurveItem() line.setPen(pg.mkPen(color=QColor(color), width=4)) line.setZValue(10) line.setData(x=[x1[0], x2[0]], y=[y1[0], y2[0]]) add_marking(line) elif el[0] == "dot": (x, ys) = el[1] dot = pg.ScatterPlotItem(x=x, y=ys[0]) dot.setPen(pg.mkPen(color=QColor(color), width=5)) dot.setZValue(10) add_marking(dot)