Displaying a previously created Matplotlib figure in a widget
See original GitHub issueI’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:
- Created 4 years ago
- Comments:8 (6 by maintainers)
@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 notThe 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 thefig.canvas
inside whatever widget I want to use (Like anHBox
). Here is an example with two figures: