Search code examples
python-2.7ipythonanacondajupyterpyqtgraph

error with IPython in showing plots with pyqtgraph


i have successfully installed pyqtgraph library in python 2.7. I forked latest project from GitHub and then python setup.py install. I am now trying to show plots with it. I open a python terminal and start typing the following:-

import pyqtgraph as pg
import numpy as np
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)

All of these commands are successfully interpreted. But then i run the command to see the plot as:-

pg.plot(x, y, symbol='o')

It outputs:

<pyqtgraph.graphicsWindows.PlotWindow at 0x6b7f708>

And then a windows titled pythonw opens and says 'not responding' and hangs and i am unable to see any output. After long time window crashes and terminal says:

Kernel died, restarting

What could be the error? Should i have installed using .exe?

EDIT: As pointed out below by titusjan, the problem is with the default Jupyter/Ipython notebook shipping with Anaconda which i have not been able to correct. There must be some installation problem. And i am working on Windows.


Solution

  • pyqtgraph plots functions based on PyQT GUI-based programming. So the task of displaying plots have to be treated as starting a GUI. As told above, when I feed the commands to the IPython terminal, it works out fine:

    import numpy as np
    import pyqtgraph as pg
    import sys
    
    x = np.random.normal(size=1000)
    y = np.random.normal(size=1000)
    

    As noted above, problem starts when i feed the line:

    pg.plot(x, y, symbol='o')
    

    To remedy this: either input these 2 lines in one go

    pg.plot(x, y, symbol='o')
    pg.QtGui.QApplication.exec_()
    

    or immediately after the previous line pg.plot(x, y, symbol='o') input this line:

    pg.QtGui.QApplication.exec_()
    

    Alternatively we could use the default QT-GUI commands also. So we get correct plots even if we run this code:-

    import numpy as np
    import pyqtgraph as pg
    from pyqtgraph.Qt import QtCore, QtGui 
    import sys
    
    x = np.random.normal(size=1000)
    y = np.random.normal(size=1000)
    pg.plot(x, y, symbol='o')
    if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
    

    However, if one wants to avoid invoking QTGui methods explicitly, assuming one has saved the below code as xyz.py, one can run the code successfully displaying the graphics by writing on the command line: pythonw -i xyz.py. This ensures that python has been explicitly asked to run in interactive mode. pythonw is for running in windows.

    import numpy as np
    import pyqtgraph as pg
    
    x = np.random.normal(size=1000)
    y = np.random.normal(size=1000)
    pg.plot(x, y, symbol='o')