Error With Ipython In Showing Plots With Pyqtgraph
Solution 1:
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 != 1ornothasattr(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')
Post a Comment for "Error With Ipython In Showing Plots With Pyqtgraph"