我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用pyqtgraph.AxisItem()。
def addAxis(self,plot,**args): """ Add an axis on the right side .. tabularcolumns:: |p{3cm}|p{11cm}| =============== ============================================================================================ **Arguments** =============== ============================================================================================ plot pyqtgraph.PlotWidget *args 1. label Label of the new axis =============== ============================================================================================ :return: pg.ViewBox """ p3 = pg.ViewBox() ax3 = pg.AxisItem('right') plot.plotItem.layout.addItem(ax3, 2, 3+len(plot.axisItems)) plot.plotItem.scene().addItem(p3) ax3.linkToView(p3) p3.setXLink(plot.plotItem) ax3.setZValue(-10000) if args.get('label',False): ax3.setLabel(args.get('label',False), color=args.get('color','#ffffff')) p3.setGeometry(plot.plotItem.vb.sceneBoundingRect()) p3.linkedViewChanged(plot.plotItem.vb, p3.XAxis) ## Handle view resizing Callback = functools.partial(self.updateViews,plot) plot.getViewBox().sigStateChanged.connect(Callback) plot.viewBoxes.append(p3) plot.axisItems.append(ax3) self.plots2D[p3]=[] # TODO do not consider a new axis as a plot. simply make it a part of the axisItems array of the main plot return p3
def tickStrings(self, values, scale, spacing): """ Defines the tick marking format based on display mode See `~pygtgraph.AxisItem` for parameter definitions. """ if self._layer is None: return super(DynamicAxisItem, self).tickStrings(values, scale, spacing) spatial_unit = self._layer.masked_dispersion.data.unit dispersion = self._layer.masked_dispersion inds = np.arange(dispersion.size, dtype=int) if self.mode == 0: c = const.c.to('{}/s'.format(spatial_unit)) waves = u.Quantity(np.array(values), spatial_unit) ref_wave = u.Quantity(self.ref_wave, spatial_unit) v_quant = ((waves - ref_wave) / waves * c).to('km/s') v = v_quant.value v[np.isnan(v)] = 0.0 self.setLabel("Velocity [{}]".format(v_quant.unit), None, None) return ["{:.4E}".format(x) for x in v] elif self.mode == 1: self.setLabel('Redshifted Wavelength [{}]'.format(spatial_unit)) return ["{:0.2f}".format(v / (1 + self.redshift) * scale) for v in values] elif self.mode == 2: self.setLabel("Pixel", None, None) inds = np.searchsorted(dispersion, values) return list(inds) return super(DynamicAxisItem, self).tickStrings(values, scale, spacing)
def __init__(self, sandbox, *args, **kwargs): pg.GraphicsLayoutWidget.__init__(self, **kwargs) self.sandbox = sandbox self.plots = [ DisplacementPlot( sandbox, title='North', component=lambda m: m.north), DisplacementPlot( sandbox, title='East', component=lambda m: m.east), DisplacementVectorPlot( sandbox, title='Down', component=lambda m: m.down), DisplacementPlot( sandbox, title='LOS', component=lambda m: m.displacement)] self.plots[-1].addHintText() self._mov_sig = pg.SignalProxy( self.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved) for ip, plt in enumerate(self.plots): row = ip / 2 col = ip % 2 + 1 self.addItem(plt, row=row, col=col) plt.showGrid(x=True, y=True) plt.hideAxis('bottom') plt.hideAxis('left') plt.vb.border = pg.mkPen(50, 50, 50) if ip != 0: plt.setXLink(self.plots[0]) plt.setYLink(self.plots[0]) def getAxis(plt, orientation, label): axis = pg.AxisItem( orientation=orientation, linkView=plt.vb) axis.setLabel(label, units='m') return axis plts = self.plots self.addItem(getAxis(plts[0], 'left', 'Northing'), row=0, col=0) self.addItem(getAxis(plts[1], 'left', 'Northing'), row=1, col=0) self.addItem(getAxis(plts[0], 'bottom', 'Easting'), row=2, col=1) self.addItem(getAxis(plts[1], 'bottom', 'Easting'), row=2, col=2) for plt in self.plots: plt.vb.menu = QtGui.QMenu(self)
def __init__(self, sandbox, *args, **kwargs): pg.GraphicsLayoutWidget.__init__(self, **kwargs) self.sandbox = sandbox self.plots = [ DisplacementPlot( sandbox, title='Scene Displacement', component=lambda m: m.reference.scene.displacement), DisplacementPlot( sandbox, title='Model Residual', component=lambda m: m.reference.difference)] self.plots[-1].addHintText() self._mov_sig = pg.SignalProxy( self.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved) for ip, plt in enumerate(self.plots): row = ip / 2 col = ip % 2 + 1 self.addItem(plt, row=row, col=col) plt.showGrid(x=True, y=True) plt.hideAxis('bottom') plt.hideAxis('left') plt.vb.border = pg.mkPen(50, 50, 50) if ip != 0: plt.setXLink(self.plots[0]) plt.setYLink(self.plots[0]) def getAxis(plt, orientation, label): axis = pg.AxisItem( orientation=orientation, linkView=plt.vb) axis.setLabel(label, units='m') return axis plts = self.plots self.addItem(getAxis(plts[0], 'left', 'Northing'), row=0, col=0) self.addItem(getAxis(plts[1], 'left', 'Northing'), row=1, col=0) self.addItem(getAxis(plts[0], 'bottom', 'Easting'), row=2, col=1) self.addItem(getAxis(plts[1], 'bottom', 'Easting'), row=2, col=2)