5

Typing

import pyqtgraph as pg
pg.plot([1,2,3,2,3])

into the standard Python REPL opens a window containing a plot of the data. Typing exactly the same code into the IPython REPL or the Jupyter console, opens no such window.

[The window can be made to appear by typing pg.QtGui.QApplication.exec_(), but then the REPL is blocked.

Alternatively, the window appears when an attempt is made to exit the REPL, and confirmation is being required.

Both of these are highly unsatisfactory.]

How can basic interactive pyqtgraph plotting be made to work with the IPython REPL?

[The described behaviour was observed in IPython 5.1.0 and Jupyter 5.0.0 with Python 3.5 and 3.6 and PyQt4 and PyQt5 (no PySide)]

jacg
  • 2,040
  • 1
  • 14
  • 27

2 Answers2

5

As sugested by @titusjan, the solution is to type

%gui qt

(or some variation on that theme: see what other options are available with %gui?) in IPython before issuing any pyqtgraph (or PyQt in general) commands.

jacg
  • 2,040
  • 1
  • 14
  • 27
2

I had a problem with matplotlib in Jupyter Notebook. It was not fast enough and I found the navigation to be deficient. I got pyqtgraph to work using tips I found here. OMG! This is a great tool. The navigation and the speed you get is awesome.

Thought I would share my solution here.

%gui qt5
from PyQt5.Qt import QApplication

# start qt event loop
_instance = QApplication.instance()
if not _instance:
    _instance = QApplication([])
app = _instance

import pyqtgraph as pg

# create and and set layout
view = pg.GraphicsView()   
view.setWindowTitle('Your title')
layout = pg.GraphicsLayout()
view.setCentralItem(layout)
view.show()

# Set white graph
pg.setConfigOptions(antialias=True)
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')

# add subplots
p0 = layout.addPlot(0,0)
p0.addLegend()
p0.plot([1,2,3,4,5], pen='b', name='p0')

p1 = layout.addPlot(1,0)
p1.addLegend()
p1.plot([2,2,2,2,], pen='r', name='p1')

p2 = layout.addPlot(1,0)
p2.addLegend(offset=(50, 0))
p2.plot([-1,0,1,1,], pen='g', name='p1.1')
p2.hideAxis('left')
p2.showAxis('right')

You will get a pop up window like that Simple PyQtGraph

PBareil
  • 157
  • 6