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
Make matplotlib inline backend the default when loaded in IPython kernel #6424
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.
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.
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?
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.
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.
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.
@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.
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).
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.
@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.
Activity
tacaswell commentedon Sep 6, 2014
I would like to put in a plug for nbagg so you get interactive as well.
takluyver commentedon Sep 6, 2014
Even better. Is it possible to set this up so that, in the kernel, it uses nbagg if available and inline otherwise?
rgbkrk commentedon Sep 6, 2014
Totally for this (as discussed in person at Berkeley today).
rgbkrk commentedon Sep 6, 2014
@tacaswell Does nbagg do vector/svg by default, then raster when too large?
tacaswell commentedon Sep 6, 2014
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
takluyver commentedon Sep 15, 2014
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?
tacaswell commentedon Sep 15, 2014
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_backendand akernel_default_backendin mpl?I am a tad wary of picking up an ipython dependency in mpl.
Sorry this is rambley.
takluyver commentedon Sep 15, 2014
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.5 remaining items
takluyver commentedon Jan 12, 2015
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.
tacaswell commentedon Jan 12, 2015
@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:
takluyver commentedon Jan 12, 2015
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.
ellisonbg commentedon Jan 12, 2015
@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.
Import hooks to set up inline backend when matplotlib is loaded