9

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?

user202729
  • 3,358
  • 3
  • 25
  • 36
Definity
  • 691
  • 2
  • 11
  • 31

3 Answers3

5

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.

EL_DON
  • 1,416
  • 1
  • 19
  • 34
  • Apart from the option of using `input()` (which requires the stdin to the connected to the terminal), if you don't have access to the `app` object or something else already constructed it, then [`QApplication.instance()` can be used to get it](https://stackoverflow.com/questions/27150852/a-qapplication-instance-already-exists). – user202729 Feb 25 '21 at 07:38
3

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.

a_guest
  • 34,165
  • 12
  • 64
  • 118
  • Even when the script has no loop such as "import pyqtgraph as pg pg.plot(data)" it still opens and closes instantly – Definity Jun 19 '17 at 15:16
  • Sure, because the script reaches its end. That is after the last statement of the script has been executed the corresponding process terminates (and so do all instantiated widgets). This is true for any Python script. Mentioning the `for` loop I just referred to your example. By using the `-i` switch you prevent the process from terminating but instead it will switch to the interactive mode. This is a "Python way" of doing it, maybe there is also a "`pyqtgraph` way" however, as mentioned, I didn't find one. – a_guest Jun 19 '17 at 15:30
0

To further a_guest answer - To enable see the graph in Pycharm goto: Run -> Edit Configurations -> Interpreter Options. Leave the -i there.

Definity
  • 691
  • 2
  • 11
  • 31