Matplotlib figures are not redrawn in the cell in which they are created

Open
#258 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by running the provided same-cell reproduction in JupyterLab or Notebook with the %matplotlib widget backend, then compare it with the separate-cell version. Done means fig.canvas.draw() visibly updates the figure during each loop iteration instead of only after the loop exits.

Written by the indexing model from the issue text.

Description

Describe the issue

Using an interactive figure's canvas.draw() method does not update the figure if it was created in the same cell as the draw call.

Figure created in the same cell:
Same cell

Figure created in another cell:
different cell

Reproduce

  1. Open a new notebook in JupyterLab or Notebook
  2. Insert the following code:
import matplotlib.pyplot as plt
import time

%matplotlib widget

fig = plt.figure()

for i in range(5):
    x = list(range(i+2))
    xx = [x**2 for x in x]
    plt.clf()
    plt.plot(x, xx)
    fig.canvas.draw()
    
    time.sleep(1)
  1. Run the cell and observe that the plot does not show until the loop has exited.

Optional:

  1. Move the for loop to a separate cell from the figure creation
  2. Run cell and observe plot updates during the loop

This can be reproduced using the scipy notebook docker stack.

Versions

3.8.5 | packaged by conda-forge | (default, Jul 31 2020, 02:39:48)
[GCC 7.5.0]
ipympl version: 0.5.7
jupyter core     : 4.6.3
jupyter-notebook : 6.0.3
qtconsole        : not installed
ipython          : 7.17.0
ipykernel        : 5.3.4
jupyter client   : 6.1.6
jupyter lab      : 2.1.5
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 5.0.7
traitlets        : 4.3.3
Known nbextensions:
  config dir: /opt/conda/etc/jupyter/nbconfig
    notebook section
      jupyter-matplotlib/extension  enabled
      - Validating: OK
      jupyter-js-widgets/extension  enabled
      - Validating: OK
JupyterLab v2.1.5
Known labextensions:
   app dir: /opt/conda/share/jupyter/lab
        @bokeh/jupyter_bokeh v2.0.3  enabled  OK
        @jupyter-widgets/jupyterlab-manager v2.0.0  enabled  OK
        jupyter-matplotlib v0.7.3  enabled  OK
Dominant language
Jupyter Notebook
Stars
1.7k
Forks
234
PR merge metrics
No merged PRs in 30d

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.

More from matplotlib/ipympl

All issues in matplotlib/ipympl

Similar issues

More Data Visualization issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.