4

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?

Gabriel Schubert
  • 165
  • 4
  • 12

3 Answers3

4

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)
Christian Karcher
  • 2,533
  • 1
  • 12
  • 17
  • Hello Christian. Thank you for your answer. I meant pyqtgraph because I was using this library (with very good results) for desktop applications. To interact with the image is very important for my goal. Do you know if there is another library that can deal with it as pyqtgraph do for desktop apps? I tried matplotlib, but it didn't give me good performance. – Gabriel Schubert Nov 27 '20 at 12:16
  • @GabrielSchubert For Jupyter I am only aware of matplotlib, which can be made interactive (https://towardsdatascience.com/how-to-produce-interactive-matplotlib-plots-in-jupyter-environment-1e4329d71651). If you want "a desktop app with pyqtgraph", why not use pyqt directly to create an application with embedded pyqtgraph functionality? Or am I misunderstanding your wish for a "desktop application"? – Christian Karcher Nov 27 '20 at 14:12
3

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
ntjess
  • 570
  • 6
  • 10
  • @Wayne thanks for the suggestion. Per your request, I've opened a PR here: https://github.com/pyqtgraph/pyqtgraph/pull/2488 – ntjess Oct 12 '22 at 18:38
1

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.

cmoman
  • 341
  • 3
  • 5