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

matploblib ignoring the switching of rendering backends #1703

Closed
amelio-vazquez-reina opened this issue Jan 25, 2013 · 7 comments
Closed

matploblib ignoring the switching of rendering backends #1703

amelio-vazquez-reina opened this issue Jan 25, 2013 · 7 comments

Comments

@amelio-vazquez-reina
Copy link

Say I have a python script that I call from IPython. I would like this script to

  • Build some plots for the sole propose of saving them to disk with fig.savefig(path_to_file), without showing them on screen.
  • Build some other plots that are displayed on the screen.

Is this currently possible within the same script?

I have tried with:

matplotlib.use('Agg')
# The following figures will not render on the screen
# but can be saved directly to disk

matplotlib.pyplot.switch_backend('QT4Agg')
# The following figures will render on the screen

matplotlib.pyplot.switch_backend('Agg')
# The following figures will not render on the screen
# but can be saved directly to disk

with no luck (matplotlib seems to ignore my calls to switch backends). Is there anything that I am doing wrong?


Also, if I have a Python script that starts with:

import matplotlib
matplotlib.use('Agg')
from pylab import *

and then I run the script for the first time, from my IPython session, and then I change the rendering backend in the script:

import matplotlib
matplotlib.use('QT4Agg')
from pylab import *

and then I run it again from the same IPython session, the changes do not seem to take effect. However, as soon as I restart IPython the backend specification is taken into consideration.

This is all with the latest stable versions of Python, matplotlib and IPython.

For reference I have also posted this issue in the IPython repository, since I am not sure which projects this issue would really belong to.

@mdboom
Copy link
Member

mdboom commented Jan 25, 2013

Just use a GUI backend, e.g. Qt4Agg and use "savefig", rather than "show" when you want to save to disk. (Assuming here that interactive mode is off).

@WeatherGod
Copy link
Member

There were some bugs fixed over the summer with respect to backend
switching. These fixes made it into v1.2.0. I think there was also some
sort of issue on the ipython side. Make sure you have the latest version
of both and see if the problem still exists.

@efiring
Copy link
Member

efiring commented Feb 11, 2013

I think the OP is running into a fundamental limitation in the way backends are handled, both in ipython and in mpl, such that you really cannot switch gui backends, or switch from a non-gui to a gui. You can switch non-gui backends--or at least this is supposed to work, but I don't think it gets much testing, if any. I don't see much point in switching non-gui backends, though, since from any backend, one can use savefig to save to any supported output format.

@mdboom
Copy link
Member

mdboom commented Feb 11, 2013

I think switching GUIs is just a hard problem, when you have multiple event loops involved, each of which wants to handle things slightly differently.

@efiring: Switching non-gui backends gets a lot of testing, since that's what happens under the hood everytime you save to a file type that is not handled natively by the backend -- it's just that calling switch_backend directly is not as necessary as it once was.

@efiring
Copy link
Member

efiring commented Feb 11, 2013

On 2013/02/11 10:52 AM, Michael Droettboom wrote:

I think switching GUIs is just a hard problem, when you have multiple
event loops involved, each of which wants to handle things slightly
differently.

Exactly. It is a major feature request for both mpl and ipython.
Certainly it would be nice to be able to switch back and forth between
inline and external plots in the ipython notebook, for example, but I
don't know whether it is feasible in a practical sense.

@efiring
Copy link
Member

efiring commented Feb 11, 2013

On 2013/02/11 10:52 AM, Michael Droettboom wrote:

@efiring https://github.com/efiring: Switching non-gui backends gets a
lot of testing, since that's what happens under the hood everytime you
save to a file type that is not handled natively by the backend -- it's
just that calling |switch_backend| directly is not as necessary as it
once was.

I'm not sure that it was ever necessary, at least during the last
several years, and I don't know what the rationale is for it at present.
I've never used it. pyplot.switch_backend() is still labeled
"experimental" in the docstring, and there is no example of its use in
the examples directory. This is what the OP was trying to use.

This is distinguished from backend_bases.switch_backends(), which is a
completely different beast, as far as I can see, and which is indeed
used all the time, and therefor is well tested. Normally it would be
used only internally, but there is one obscure example of its use at the
user level: pylab_examples/to_numeric.py.

@tacaswell
Copy link
Member

Closing this as it has had no action in the last year and will probably involve a major overhaul of the backend system to fix.

This maybe addressed in how @mdehoon suggests re-doing the event loop.

I see the advantage of being able to switch from inline to not-inline in ipython notebooks, but that involves a lot of code which is theirs. I do not see why you would want to change the gui backend mid-stream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants