Skip to content

What prevents a figure showing before notebook cell completion? #290

@thomasaarholt

Description

@thomasaarholt

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions