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.

What prevents a figure showing before notebook cell completion?

See original GitHub issue

My issue boils down to:

“Why does the figure not show immediately, but instead waits until the cell has completed running?”

%matplotlib widget
import matplotlib.pyplot as plt
from time import sleep
fig = plt.figure()
plt.plot([1,2,3])
sleep(2)
Figure only showing at end of sleep/cell

This behaviour is preventing fun uses, like dynamic plotting in a single cell:

%matplotlib widget
from matplotlib import pyplot as plt
from time import sleep
import numpy as np
fig = plt.figure()

for i in range(10):
    x, y = np.random.random(2)
    plt.scatter(x, y)
    fig.canvas.draw()
    sleep(0.1)

The previous code works if one splits it into two cells, placing the figure() call in the first one:

# Cell 1
%matplotlib widget
from matplotlib import pyplot as plt
from time import sleep
import numpy as np
fig = plt.figure()

# Cell 2
for i in range(10):
    x, y = np.random.random(2)
    plt.scatter(x, y)
    fig.canvas.draw()
    sleep(0.1)
Dynamic plotting if using two cells

Edit: I also note that in the qt backend, there figure and plot are shown immediately, in contrast to the ipympl behaviour.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
ianhicommented, Feb 3, 2022

A workaround that works with the current version is:

%matplotlib widget
from matplotlib import pyplot as plt
from time import sleep
import numpy as np

def display_immediately(fig):
    canvas = fig.canvas
    display(canvas)
    canvas._handle_message(canvas, {'type': 'send_image_mode'}, [])
    canvas._handle_message(canvas, {'type':'refresh'}, [])
    canvas._handle_message(canvas,{'type': 'initialized'},[])
    canvas._handle_message(canvas,{'type': 'draw'},[])
    
    
with plt.ioff():
    fig = plt.figure()

display_immediately(fig)

for i in range(10):
    x, y = np.random.random(2)
    plt.scatter(x, y)
    fig.canvas.draw()
    sleep(0.1)

1reaction
ianhicommented, Sep 23, 2021

And it seems to work fine. The first animation won’t have the correct DPI, but the next ones will have the right one (because we now save the DPI as a static Canvas property)

I think guessing a DPI of 1 is a reasonable strategy? and then once the communication of the true DPI happens we can immediately redraw in the case that the DPI is different. Although faking the messages like this may not be strictly optimal instead of just calling the underlying methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Require Jupyter notebook to render matplotlib notebook figure ...
The simplest workaround is start from the top and manually step through each cell, wait for it to render, and then move onto...
Read more >
15 Tips and Tricks for Jupyter Notebook that will ease your ...
15 Tips and Tricks for Jupyter Notebook that will ease your Coding Experience · 1. Calculate the time of execution of a cell:...
Read more >
Jupyter Notebook 6.5.2 documentation - Read the Docs
A code cell allows you to edit and write new code, with full syntax highlighting and tab completion. The programming language you use...
Read more >
Interactive figures — Matplotlib 3.6.2 documentation
Matplotlib keeps a reference to all of the open figures created via ... pyplot.show() runs the GUI event loop and does not return...
Read more >
How to enable Autocompletion in Jupyter Notebook application
character. RDP with IPython Autocompletion. This IPython command also enables the "shift + tab" buttons to show the function help and ...
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