question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Displaying a previously created Matplotlib figure in a widget

See original GitHub issue

I’m trying to find a way to take a figure previously created, and show it inside a widget, without losing interactivity. Here’s a short version of my approach (which may not be the right one):

fig = plt.figure() # previously created figure
out = widgets.Output()
with out:
    display(fig)
# other things happening
display(widgets.HBox([fig.canvas, some_other_fig]))

If I first create a figure in the notebook outside of an Output widget, and then wish to include it later inside something like an HBox next to a second figure, I seem to lose interactivity with the figure (including the nice ipympl zoom interface).

Here is a nbviewer view of a MWE notebook I created, download it top right on the window. It’s best if one downloads it and runs it since the first figure appears buggy in the nbviewer.

Additionally, and I might create a new issue for this - if I call fig1 at the end of the notebook, I get an unexpected error. Here I’d expect the regular figure to be shown. The error is AttributeError: 'NoneType' object has no attribute '_send_event'.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ianhicommented, Jul 1, 2022

@alexrockhill i think there was a typo in the first post’s example. It should be fig.canvas in the HBox (i’ve just edited the first post). The canvas inherits from widget while figure does not

1reaction
thomasaarholtcommented, Mar 18, 2020

The following is my own solution. I turn off matplotlib interactive mode, which prevents the figure from being shown immediately after calling fig = plt.figure(). Then I place the fig.canvas inside whatever widget I want to use (Like an HBox). Here is an example with two figures:

%matplotlib widget
from IPython.display import display # this is automatically imported in the notebook anyway
from ipywidgets.widgets import HBox
import matplotlib.pyplot as plt
from scipy.misc import face

plt.ioff() # turn off interactive mode so figure doesn't show
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
plt.ion() # figure still doesn't show

ax1.imshow(face())
ax2.imshow(face(True))

display(HBox([fig1.canvas, fig2.canvas]))

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Displaying a previously created Matplotlib figure in a widget
I'm trying to find a way to take a previously created Matplotlib figure, and show it inside a widget. Here's a short version...
Read more >
How to display matplotlib plots in a Jupyter tab widget?
1 on Ubuntu 16.04 inside a virtual environment.) If I run it for a second time, or if I rerun it putting everything...
Read more >
Ipywidgets with matplotlib - Kapernikov
We will create a matplotlib figure again, but this time inside an Output widget. Output can take all kinds of input and display...
Read more >
Matplotlib Widgets — How to Make Your Plot Interactive With ...
This article presents different types of widgets that can be embedded within a matplotlib figure, in order to create and personalize highly interactive ......
Read more >
matplotlib.widgets — Matplotlib 3.6.2 documentation
Widgets that are designed to work for any of the GUI backends. All of these widgets require you to predefine a matplotlib.axes.Axes instance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found