Skip to content

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

Closed

Description

@jorisvandenbossche

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

Activity

  1. added this to the milestone on Mar 26, 2015
  2. tacaswell commented on Mar 26, 2015

    @tacaswell
    Member

    It should behave like the GUI backends.

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

    attn @pelson

  3. pelson commented on Mar 26, 2015

    @pelson
    Member

    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?

  4. jorisvandenbossche commented on Mar 26, 2015

    @jorisvandenbossche
    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.

  5. costerwi commented on Apr 30, 2015

    @costerwi

    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

  6. tritemio commented on May 21, 2015

    @tritemio

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

  7. tacaswell commented on May 22, 2015

    @tacaswell
    Member

    The workaround is plt.close('all')

  8. self-assigned this
    on May 22, 2015
  9. mangecoeur commented on Jun 1, 2015

    @mangecoeur

    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.

  10. mangecoeur commented on Jun 2, 2015

    @mangecoeur

    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.

  11. tacaswell commented on Jun 2, 2015

    @tacaswell
    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)
    .

  12. modified the milestones: , on Oct 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions