What prevents a figure showing before notebook cell completion?
See original GitHub issueMy 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)

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)

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:
- Created 3 years ago
- Reactions:1
- Comments:10 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A workaround that works with the current version is:
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.