matplotlib / matplotlib/ipympl

Displaying a previously created Matplotlib figure in a widget

Open
#203 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
1.7k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

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

```python
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](https://nbviewer.jupyter.org/github/thomasaarholt/tests/blob/master/Output%20Example%20Notebook.ipynb) 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'`.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Download and run the linked Output Example Notebook to reproduce the behavior with the previously created figure, Output widget, and HBox. Start by tracing how the displayed figure and its canvas are handled, then verify that the figure retains ipympl interactivity in the HBox and that displaying fig1 no longer raises the reported NoneType error.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, matplotlib, python
Domain
data-visualization, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.