PyQtGraph opening then closing straight away
You can keep the window open by creating a QApplication
at the beginning of the script and then calling its exec_()
method at the end of your script, like this:
import pyqtgraph as pg
import numpy as np
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv) # Create QApplication ***
x = np.arange(1000)
y = np.random.normal(size=(3, 1000))
plotWidget = pg.plot(title="Three plot curves")
for i in range(3):
plotWidget.plot(x, y[i], pen=(i, 3))
# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
app.exec_() # Start QApplication event loop ***
I put *** on the key lines.
The problem is that the Python process finishes after the last iteration of the for
loop and thus also terminates the widgets. You can use the -i
switch in order to enter the interactive Python interpreter after executing the script which retains all objects that were instantiated during execution of the script:
python -i /path/to/script.py
Admittedly this is rather a workaround and pyqtgraph
probably has a "native" way of achieving this (such as the show
function from matplotlib.pyplot
does it by default) however I couldn't find a similar function for pyqtgraph
.