Search code examples
pythonpyqtpyqtgraph

PyQtGraph opening then closing straight away


I'm running some basic code from the documentation

import pyqtgraph as pg
import numpy as np
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))

But for some reason the window open then closes straight away, i just see a flicker of it. Is there some sort of function in which i can keep the window open?


Solution

  • 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.