I'm using PyQt5 and PyQtGraph. I have not been able to figure out how to clear the ZOOM-level of a plot in PyQtGraph PlotWidget. The following code demonstrates. It draws a red rectangle. Then use the mouse wheel to zoom-out. Then click "Button1" and the code calls the PlotWidget.clear() method which does clear the plotted data and redraws the same rectangle blue, but it does NOT clear the ZOOM-level. I assumed that PlotWidget.clear() would clear the entire plot including the ZOOM-level, and I could not find a PlotWidget method to clear the ZOOM-level. See the comment "#??? why doesn't this clear the 'ZOOM'" in the code below on line 45.
import sys
from PyQt5 import QtWidgets
import numpy as np
import pyqtgraph as pg
from pyqtgraph import PlotWidget, plot
# *********************************************************************************************
# *********************************************************************************************
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("My MainWindow")
self.qPlotWidget = pg.PlotWidget(self)
self.qPlotWidget.setLabel("bottom", "X-Axis")
self.qPlotWidget.setLabel("left", "Y-Axis")
self.qPushButton = QtWidgets.QPushButton(self)
self.qPushButton.setText("Button1")
self.qPushButton.clicked.connect(self.qPushButtonClicked)
self.toggle = 0
self.plotData()
def plotData(self):
data1 = np.zeros((5, 2), float) # create the array to hold the data
data1[0] = np.array((1.0, 10.0))
data1[1] = np.array((1.0, 20.0))
data1[2] = np.array((2.0, 20.0))
data1[3] = np.array((2.0, 10.0))
data1[4] = np.array((1.0, 10.0))
if(self.toggle == 0):
pen1 = pg.mkPen(color=(255,0,0), width=2) # red
self.toggle = 1
else:
pen1 = pg.mkPen(color=(0,0,255), width=2) # blue
self.toggle = 0
self.qPlotWidget.plot(data1, pen=pen1, name="data1")
def qPushButtonClicked(self):
print("In qPushButtonClicked")
self.qPlotWidget.clear() # ??? why doesn't this clear the 'ZOOM'
self.plotData()
def resizeEvent(self, event):
size = self.geometry()
self.qPlotWidget.setGeometry(50, 50, size.width()-100, size.height()-100)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
screen = QtWidgets.QDesktopWidget().screenGeometry()
w.setGeometry(100, 100, screen.width()-200, screen.height()-200) # x, y, Width, Height
w.show()
sys.exit(app.exec_())