You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Matplotlib figures are not redrawn in the cell in which they are created #258
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:
Figure created in another cell:
Reproduce
Open a new notebook in JupyterLab or Notebook
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)
Run the cell and observe that the plot does not show until the loop has exited.
Optional:
Move the for loop to a separate cell from the figure creation
@kjgoodrick sorry I pointed you this way and then never followed up.
I don't know why this is happening exactly but I suspect that it is some oddities of timing of the matplotlib backend doing work to create the figure and the wild world of async and python kernel <-> javascript conncetions. So when you put the loop in a separate cell you get the result you expect because three is enough of a pause between cell executions for ~~ something ~~ (message buffers flushing?) to happen. You can see this if you take your two cell example and autorun the cells using the Restart and Run All button. When you do this the cells are executed nearly immediately one after the other and you see the same result.
No worries! That example is very interesting. I think you are right that it is something to do with some async operations, I asked about this on stack overflow first and someone there was able to solve it by running the plotting loop inside the IPython event loop. I'm not sure if that's a generic way to fix things, but it at least shows it is possible.
Activity
ianhi commentedon Sep 8, 2020
@kjgoodrick sorry I pointed you this way and then never followed up.
I don't know why this is happening exactly but I suspect that it is some oddities of timing of the matplotlib backend doing work to create the figure and the wild world of async and python kernel <-> javascript conncetions. So when you put the loop in a separate cell you get the result you expect because three is enough of a pause between cell executions for ~~ something ~~ (message buffers flushing?) to happen. You can see this if you take your two cell example and autorun the cells using the
Restart and Run Allbutton. When you do this the cells are executed nearly immediately one after the other and you see the same result.kjgoodrick commentedon Sep 9, 2020
Hi @ianhi,
No worries! That example is very interesting. I think you are right that it is something to do with some async operations, I asked about this on stack overflow first and someone there was able to solve it by running the plotting loop inside the IPython event loop. I'm not sure if that's a generic way to fix things, but it at least shows it is possible.