-
Notifications
You must be signed in to change notification settings - Fork 228
Re-executing a cell hides the figure when it is given a num=...
argument
#405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for reporting this issue. For some reason, the issue doesn't appear if the activation of the backend is done in the same cell: %matplotlib widget
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi/2, np.pi/2)
plt.figure(num=4)
plt.plot(x, np.sin(x)); |
This got kinda long the tldr is: I think this is actually a bug in nbagg and the best solution is probably to teach your students to use the explicit (nee OO) interface like so: fig1, ax1 = plt.subplots()
ax1.plot(...)
Inline probably isn't the best comparison because it displays and then closes all figures at the end of every cell. I'm a little bit surprised by the behavior of nbagg though. As I look at this more though I think this may actually be a bug in nbagg? My understanding of the behavior of For example if you run the following two cells: cell 1 %matplotlib nbagg
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi/2, np.pi/2)
i=0 cell 2 (run this one repeatedly) plt.figure(num=1)
plt.plot(x, np.sin(x*i))
i += 1 then you only ever have a plot with a single line in it. but I think the correct behavior should be to have multiple lines show up. To get that you can do the equivalent from ipython script import matplotlib.pyplot as plt
import numpy as np
plt.ion()
x = np.linspace(-np.pi/2, np.pi/2)
# each iteration of loop represents a cell re-execution
i = 0
def run_cell():
global i
plt.figure(num=1)
plt.plot(x, np.sin(x*i))
i += 1
I think this is because setting the backend closes all figures so you are no longer refering to the same figure. |
That's a bit short of an explanation - happy to talk more about this if I can be helpful! |
@ianhi Thank you for all the insightful comments! I'm going to experiment with this, before asking more questions. |
As @jklymak and @QuLogic pointed out in matplotlib/matplotlib#21957 the issue is with the life cyle management of the implicit figures. In the case of nbagg we have logic that when the js side is removed from view we "close" the figure and drop it from Matplotlib's registry of open figures. From the behavior I think the order of things is output is cleared -> call back from js-to-kernel is run to close the figure -> cell is run -> matplotlib finds there is no figure 1 makes a new one -> new figures are shown at the end of cell execution. In the IPython + GUI case because the window is never closed, each time through the loop we find that after the first pass there exists a figure 1, As @ianhi notes with inline it always closes the figure after rendering the cell so it always makes a new one. In both the nbagg and ipympl cases if you create the figure in one cell, and then repeatedly do Of these semantics, it is not clear which one is "right" and how this should interact with having multiple views of the same output. Also see the discussion in #171 |
After trying a few things, I found
Comparing all these, a modification of ipympl's behavior to match that of nbagg, seems ideal, but I understand that that is a lot of work. For the time being, calling |
completely off-topic: the explicit (aka Object Oriented) API is "new" as of ~15 years ago ;) Try: fig, ax = plt.subplots(num=1, clear=True)
ax.plot([0, 1], [2, 3])
plt..show() instead. |
Thanks for your help! That works indeed, but I'm still puzzled by how ipympl decides what to show when In comparison, the behavior of calling P.S. Yes, "very old" versus "old" would have been more fitting. :] |
Describe the issue
Explicit figure numbers seem to cause a small glitch in
ipympl
. When the following cell is executed once, it shows the plot. When re-executing the cell, it disappears again.I've attached a working example: repeat_plot_issue.zip
The
inline
andnotebook
backends do not have this issue, which leads me to believe this is a bug. There are two workarounds:plt.close(1)
plt.show()
(I'm using Jupyter notebooks with a group of about 40 students, and they got confused by this difference in behavior between the backends. I understand this is not a major issue because there are simple workarounds. Still, for newcomers, it is an extra difficulty: "Help, my plot has disappeared!")
Versions
The text was updated successfully, but these errors were encountered: