Skip to content

Make matplotlib inline backend the default when loaded in IPython kernel #6424

Closed as not planned

Description

@takluyver

I think when using matplotlib in the kernel, we want the inline backend more often than not. This is especially true in the notebook, where saving figures in the output is part of the point. I'm a bit hazy on how to achieve this, but I think it's worth doing.

Prompted by jupyter/docker-notebook#7

Activity

  1. tacaswell commented on Sep 6, 2014

    @tacaswell
    Contributor

    I would like to put in a plug for nbagg so you get interactive as well.

  2. takluyver commented on Sep 6, 2014

    @takluyver
    MemberAuthor

    Even better. Is it possible to set this up so that, in the kernel, it uses nbagg if available and inline otherwise?

  3. rgbkrk commented on Sep 6, 2014

    @rgbkrk
    Member

    Totally for this (as discussed in person at Berkeley today).

  4. rgbkrk commented on Sep 6, 2014

    @rgbkrk
    Member

    @tacaswell Does nbagg do vector/svg by default, then raster when too large?

  5. tacaswell commented on Sep 6, 2014

    @tacaswell
    Contributor

    nbagg is always rasterized (as it uses the AGG backend), but you can easily save the images as vector graphics as you want using savefig('foo.svg'). It is some what clever and ships png deltas across the wire instead of the full image every time.

    As we have an svg backend, it should be possible to hook that into the notebook as well, but I fear exactly how to do that is currently over my head on the webby side.

    cc @pelson @mdboom

  6. takluyver commented on Sep 15, 2014

    @takluyver
    MemberAuthor

    We decided today that it does make sense for inline to be the default backend in the kernel. For the moment, we prefer this to nbagg, because inline doesn't assume a particular frontend - it works in the Qt console, roughly works in the terminal console, and it's much easier for third party frontends to support.

    However, I think this can be done more neatly from the mpl side, by detecting if it's loaded within an IPython kernel and picking a different default backend, than from the IPython side. @minrk has an import hook which sets the default backend when matplotlib is loaded, but that seems like the wrong way to do this. @tacaswell, what do you think?

  7. tacaswell commented on Sep 15, 2014

    @tacaswell
    Contributor

    I am a bit behind on all of the terminology (and day-to-day use ipython running in emacs). My understanding is the the kernel is the backend that has the live interpreter which talks to a variety of front ends (and this is where jupiter kernels come in that now the front ends can talk to any thing which speaks ipython-wire-protocol). The issue here is what backend the kernel should use by default and the problem with nbagg is that it assumes that it is talking to a web-front end (with all the JS magic). If the above is wrong, the rest of this should probably be ignored.

    It sounds like what you really want is a backend that can tell if it has a web-front end and decay gracefully if it does not.

    It is possible to change the backend being used (which is how saving works), so the right thing to do may be for the web-front end to set the backend of the kernel to nbagg when it connects (or maybe on a cell-to-cell basis). There are currently checks against this from pyplot, but that might be fixable.

    Why does doing it with an import hook seem wrong?

    Is it easy to check if it is being imported from a kernel? Is the idea that there is a default_backend and a kernel_default_backend in mpl?

    I am a tad wary of picking up an ipython dependency in mpl.

    Sorry this is rambley.

  8. takluyver commented on Sep 15, 2014

    @takluyver
    MemberAuthor

    Your understanding is correct. :-)

    For the moment, the model is that the kernel does not know about what frontend is running the current piece of code, and it's entirely possible to have multiple frontends connected to one kernel at the same time. We're considering giving the kernel more info about the frontend, but that's not in place yet. In the meantime, there could be a backend which produced both the HTML view for nbagg, and the static png like inline - frontends automatically select the mime type they're best able to display.

    It is easy to check if you're in a kernel, and it's possible without adding a hard dependency on IPython (start with if 'IPython' in sys.modules). And yes, my thinking was to add a secondary config option for the default backend when in the IPython kernel.

  9. 5 remaining items

  10. removed this from the 2.4 milestone on Jan 12, 2015
  11. takluyver commented on Jan 12, 2015

    @takluyver
    MemberAuthor

    I think it needs to happen on the mpl side; it seems much cleaner for mpl to check if it's in an IPython kernel than for IPython to watch for imports of matplotlib. Plus it's more applicable to other plotting packages.

  12. tacaswell commented on Jan 12, 2015

    @tacaswell
    Contributor

    @minrk mentioned he has an import hook that will take care of this.

    It is not enough to know if we are in an IPython kernel, we need to know if
    the front-end is an ipython notebook for all the js magic to work so I
    think that the logic should live on the ipython side as it was my
    understanding that you shouldn't be able to tell which front end is being
    used from the kernel.

    On Mon Jan 12 2015 at 2:08:25 PM Thomas Kluyver notifications@github.com
    wrote:

    I think it needs to happen on the mpl side; it seems much cleaner for mpl
    to check if it's in an IPython kernel than for IPython to watch for imports
    of matplotlib. Plus it's more applicable to other plotting packages.

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

  13. takluyver commented on Jan 12, 2015

    @takluyver
    MemberAuthor

    I think the backend should default to the plain inline backend, rather than the fancy nbagg, for precisely this reason (or there could be a composite backend that produces both formats). Our kernel machinery doesn't know which frontend it's currently talking to any more than code running in the kernel does, and we don't want it to. It's not even really a logical question to ask: there may be different frontends receiving the same output, and it may be saved and nbconverted into lots of different formats.

  14. ellisonbg commented on Jan 12, 2015

    @ellisonbg
    Member

    @takluyver has a good point. The inline backend is a really safe option - it doesn 't require any of the JavaScript stuff and is easy to change on the fly to another backend if needed.

    I think it is a reasonable approach to simply see if the code is running in an IPython kernel and then enable the inline backend by default.

  15. modified the milestones: 4.0, 3.0 on Jan 20, 2015
  16. modified the milestones: 4.0, wishlist on Jun 16, 2015
  17. self-assigned this
    on Dec 3, 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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions