Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get basic interactive pyqtgraph plot to work in IPython REPL or Jupyter console?

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)]

like image 520
jacg Avatar asked Apr 15 '17 10:04

jacg


People also ask

How do you display plots in Jupyter notebook?

Jupyter Notebook - Big Data Visualization Tool IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called.

What is PlotWidget?

The plot widget is used to graphically display data. The plotting panel can be added to the dashboard by opening up the "file" option on the top left of the dashboard, then selecting "Add Widget" (Figure 1) and clicking on "Plot Panel" (Figure 2).

How do you zoom a plot on a Jupyter notebook?

Just drag the right bottom corner of the plot..

How do I plot data in pyqtgraph?

There are a few basic ways to plot data in pyqtgraph: pyqtgraph.plot () Create a new plot window showing your da ... PlotWidget.plot () Add a new set of data to an existing plo ... PlotItem.plot () Add a new set of data to an existing plo ... GraphicsLayout.addPlot () Add a new plot to a grid of plots

What is pyqtgraph used for?

PyQtGraph uses Qt's QGraphicsScene to render the graphs. This gives us access to all the standard Qt line and shape styling options for use in plots. However, PyQtGraph provides an API for using these to draw plots and manage the plot canvas.

What are the styling options available in pyqtgraph?

PyQtGraph uses Qt's QGraphicsScene to render the graphs. This gives us access to all the standard Qt line and shape styling options for use in plots. However, PyQtGraph provides an API for using these to draw plots and manage the plot canvas. Below we'll go through the most common styling features you'll need to create and customize your own plots.

How to make Jupyter Notebook use qt5agg interactive backend?

Magic command %matplotlib makes jupyter notebook use Qt5Agg interactive back end. Show activity on this post. To imitate a blocking call to plt.show () with interactive backend you need to wait until your figure is active. The exception case closes the figure's window when a user presses Interrupt the kernel (aka Stop) button in Jupyter Notebook.


2 Answers

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.

like image 194
jacg Avatar answered Nov 01 '22 10:11

jacg


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

like image 42
PBareil Avatar answered Nov 01 '22 11:11

PBareil