Skip to content
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

nbagg: "Close figure" action does not 'close' the figure object #4281

Closed
jorisvandenbossche opened this issue Mar 26, 2015 · 9 comments · Fixed by #4456
Closed

nbagg: "Close figure" action does not 'close' the figure object #4281

jorisvandenbossche opened this issue Mar 26, 2015 · 9 comments · Fixed by #4456
Assignees
Milestone

Comments

@jorisvandenbossche
Copy link

I am not fully sure how the notebook backend is supposed to work (and I also did not found any documentation on it?), but the following did surprise me a bit:

%matplotlib notebook

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0, 2*np.pi, 1024)
plt.plot(t, np.sin(t), t, -np.sin(t))

The above creates an interactive figure in the notebook.
But when I close it (the red button with cross) and the figure is displayed in the notebook as a static image, a subsequent plotting call does nothing:

plt.plot(t, np.sin(t), t, np.cos(t))

It is only when I explicitely create a new figure that it is displayed:

plt.figure()
plt.plot(t, np.sin(t), t, np.cos(t))

or if I explicitly close the previous figures with e.g. plt.close('all').

So to me it seems like the plt.plot call is still seeing the original figure as the current figure and so trying to plot it on this original figure, but as this is already displayed as a static image in the previous cell, this does not do anything.

I don't know if this is on purpose, but e.g. with the qt or gtk backend when you close a figure, plt.plot will create a new figure automatically, and not trying to plot on the current figure anymore (as this is closed).


matplotlib 1.4.3, ipython 3.0, windows 7

@tacaswell tacaswell added this to the Color overhaul milestone Mar 26, 2015
@tacaswell
Copy link
Member

It should behave like the GUI backends.

It sounds like one of the call backs is not quite right.

attn @pelson

@pelson
Copy link
Member

pelson commented Mar 26, 2015

Hmmm. I remember our UAT document explicitly covers these kinds of scenarios.
@jorisvandenbossche - can you try out https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/web_backend/nbagg_uat.ipynb and see that it is behaving as you expect?

@jorisvandenbossche
Copy link
Author

I think this particular case is not included in the UAT notebook.

You have UAT 5 - No show without plt.show in non-interactive mode, but I was working in interactive mode (I used %matplotlib notebook).
You have UAT 8 - Interactive mode, but this only tests that an subsequent line is plotted on the original figure, not that a new plot is generated for that line if the previous figure was closed.

@costerwi
Copy link

In my experience:

  • Executing the cell containing only plt.plot(t, np.sin(t), t, np.cos(t)) will create a nice interactive plot but the selection remains in that same cell, unlike inline or Qt4Agg where the selection moves on to the next cell for execution.
  • Executing the same cell a second time makes the figure disappear and the selection moves to the next cell for execution. Repeated executions will not display anything until after a plt.close() command, as stated above. Each time plt.close() is executed in this condition, a new error appears on the console:

[IPKernelApp] ERROR | No such comm: 3d1517c3278342538a577515ab22e5a1

@tritemio
Copy link

Is there any workaround for this problem? This is preventing me to use the notebook backend in an introductory workshop.

@tacaswell
Copy link
Member

The workaround is plt.close('all')

@mangecoeur
Copy link

Have a similar issue - originally posted in ipython, copying essential here. The behaviour on calling close differs depending on whether you put it in the same cell or a different cell, which to me seems weird. If you do:

%matplotlib notebook
f = plt.figure()
plt.plot([1,2,3], [1,2,3])

-> figure appears
--> new cell

plt.close(f)
plt.gcf()

-> figure image appears (non interactive)

However if you group the commands in a single cell:

%matplotlib notebook
f = plt.figure()
plt.plot([1,2,3], [1,2,3])
plt.close(f)
plt.gcf()

No image appears (and the layout is also somewhat broken - using Firefox). Calling gcf() in the next cell does also not show any image (unlike with the previous case). Sometimes you get the previously plotted image if you don't reset the backend by calling %matplotlib notebook.

@mangecoeur
Copy link

Also, some people have mentioned that using the oo interface is preferred, but if you are using pandas or seaborn for plotting they work in a procedural way and you don't have control over that, so it still needs to work in procedural mode.

@tacaswell
Copy link
Member

I believe that all of the pandas plotting function take ax as a keyword
argument so that is not a problem. I don't know if seaborn does as well,
but if it does not then this should be reported as a bug against their
project.

On Tue, Jun 2, 2015 at 6:27 AM mangecoeur notifications@github.com wrote:

Also, some people have mentioned that using the oo interface is preferred,
but if you are using pandas or seaborn for plotting they work in a
procedural way and you don't have control over that, so it still needs to
work in procedural mode.


Reply to this email directly or view it on GitHub
#4281 (comment)
.

@tacaswell tacaswell modified the milestones: Color overhaul, next major release (2.0) Oct 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants