Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore zoom to default level after mouse interactions in pyqtgraph

Is there a way to restore the default zoom level for pyqtgraph plots. I know about the small button that is displayed in the plot (bottom left corner of the plot) that restores the default zoom level. What I need is I want to restore it (after I zoom in or zoom out), from the code, when a particular event happens.

I checked the pyqtgraph documentation but couldn't find any similar functions.

Here is the minimum reproducible code:

from PyQt5 import QtGui  
import pyqtgraph as pg
import numpy as np

app = QtGui.QApplication([])
w = QtGui.QWidget()
btn = QtGui.QPushButton('press me')
text = QtGui.QLineEdit('enter text')
listw = QtGui.QListWidget()
plot = pg.PlotWidget()

layout = QtGui.QGridLayout()
w.setLayout(layout)
layout.addWidget(btn, 0, 0) 
layout.addWidget(text, 1, 0) 
layout.addWidget(listw, 2, 0) 
layout.addWidget(plot, 0, 1, 3, 1) 

x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
plot.plot(x, y, pen=(2,3), symbol='d')

w.show()

app.exec_()
like image 245
tonyjosi Avatar asked Sep 17 '25 01:09

tonyjosi


1 Answers

If the source code is analyzed, it is observed that when the button you indicate is pressed, the enableAutoRange() method is called, so that is the method that has to be used:

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import numpy as np

if __name__ == "__main__":

    app = QtGui.QApplication([])
    w = QtGui.QWidget()
    btn = QtGui.QPushButton("press me")
    text = QtGui.QLineEdit("enter text")
    listw = QtGui.QListWidget()
    plot = pg.PlotWidget()

    btn.clicked.connect(lambda: plot.getPlotItem().enableAutoRange())

    layout = QtGui.QGridLayout()
    w.setLayout(layout)
    layout.addWidget(btn, 0, 0)
    layout.addWidget(text, 1, 0)
    layout.addWidget(listw, 2, 0)
    layout.addWidget(plot, 0, 1, 3, 1)

    x = np.random.normal(size=1000)
    y = np.random.normal(size=1000)
    plot.plot(x, y, pen=(2, 3), symbol="d")

    w.show()

    app.exec_()
like image 199
eyllanesc Avatar answered Sep 18 '25 15:09

eyllanesc