PyQtGraph How to zoom in using a button

Viewed 547

I'm using PyQTGraph along with PyQT5, and I've added a GraphicsLayoutWidget, with mouse events to add markers.

canvas_ruler = pg.GraphicsLayoutWidget()
formLayout.addWidget(self.canvas_ruler)
plot_ruler = canvas_ruler.addPlot(name="Ruler")

plot_ruler.hideAxis('left')

canvas_ruler.scene().sigMouseMoved.connect(self.mouseMoved)
canvas_ruler.scene().sigMouseClicked.connect(self.mouseClicked)
plot_ruler.setMouseEnabled(x=True, y=False)

What I would like to add, are 2 buttons, to zoom in and zoom out, currently the mouse wheel event, does zooming in/out. But I want to disable that and add 2 buttons for it. I probably should:

setMouseEnabled(x=False, y=False)

But I don't know how to trigger the zoom using code, I couldn't find it's APIs, like sigMouseMoved.connect.

1 Answers

The nearest native thing pyqtgraph does that approaches this (AFAIK) is the zoom with the mouse wheel feature. The MouseWheelEvent() function in ViewBox achives this with a call to the ViewBox function scaleBy().

You can see the source for all inputs and more detail but the main one is s (scale) and center (center around which to zoom). Leave center as None to zoom around the center of the plot.

Pass a tuple of x and y zoom scales to the function to zoom, < 1 for in and > 1 for out. e.g.:

    def handle_plot_zoom_buttons(self, in_or_out):
        """
        see ViewBox.scaleBy()
        pyqtgraph wheel zoom is s = ~0.75
        """
        s = 0.9
        zoom = (s, s) if in_or_out == "in" else (1/s, 1/s)
        self.plot.vb.scaleBy(zoom)
Related