Is it possible to use pyqtgraph functions to plot inline on Jupyter Notebook?

Question:

I am trying to use Pyqtgraph to plot some image arrays on Jupyter notebook, but I am having some problems. Basically, I’m using the following code:

import pyqtgraph as pg
%gui qt
pg.image(recon_array_a[0])

This is giving me what I need but is opening it in a new window. Is there a way to open inline? Like is possible to do using matplotlib on Jupyter notebook, for exemple?

Asked By: Gabriel Schubert

||

Answers:

I am not aware of a way to make interactive qt applications such as pyqtgraph accessible inline in a jupyter notebook.

If you are only interested in the "raw image" without any interactivity, here’s a workaround for you:

  • generate an image with pyqtgraph
  • export this image to a temporary file
  • display the temporary file

Here’s a quick demo (%gui qt is not required anymore)

import pyqtgraph as pg
import pyqtgraph.exporters
import numpy as np
from IPython.display import display, Image

def show_image(data):
    img = pg.image(data)
    file_name = "temp.png"
    exporter = pg.exporters.ImageExporter(img.imageItem).export(file_name)
    img.close()
    display(Image(filename=file_name))

data = np.array([[1,0],[2,3]])
show_image(data)
Answered By: Christian Karcher

A contributor in this thread had some success

https://groups.google.com/g/pyqtgraph/c/KImnC-hOTMA

QT Console may also help get the communication working between Jupyter and Python

https://qtconsole.readthedocs.io/en/stable/

Alternatively

https://bokeh.org/ and https://plotly.com/ might be options for you to consider.

Answered By: cmoman

Pyqtgraph contributor here.

For future visitors, somewhat recent updates allow embedding some pyqtgraph widgets into notebooks via jupyter_rfb. Check out the binder instance for examples.

Answered By: ntjess