在pyqtgraph中鼠标交互后将缩放恢复到默认级别 [英] Restore zoom to default level after mouse interactions in pyqtgraph
问题描述
有没有办法恢复 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.
我查看了 pyqtgraph
文档,但找不到任何类似的功能.
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_()
推荐答案
If 源代码分析,观察到当你指示的按钮被按下时,enableAutoRange()
方法被调用,所以这是必须使用的方法:
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_()
这篇关于在pyqtgraph中鼠标交互后将缩放恢复到默认级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!