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

%matplotlib magic does not respect matplotlib's backend selection mechanism #10255

Open
anntzer opened this issue Feb 7, 2017 · 3 comments
Open
Milestone

Comments

@anntzer
Copy link
Contributor

anntzer commented Feb 7, 2017

(null is an empty profile)

$ MPLBACKEND=tkagg ipython --profile=null
Python 3.6.0 (default, Jan 16 2017, 12:12:55) 
Type "copyright", "credits" or "license" for more information.

IPython 5.2.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

IPython profile: null

In [1]: %matplotlib
Using matplotlib backend: Qt5Agg

Ideally %matplotlib would respect MPLBACKEND.

It appears that the problem happens in pylabtools.find_gui_and_backend, namely

        # We need to read the backend from the original data structure, *not*
        # from mpl.rcParams, since a prior invocation of %matplotlib may have
        # overwritten that.
        # WARNING: this assumes matplotlib 1.1 or newer!!
        backend = matplotlib.rcParamsOrig['backend']  ### <- oops

The correct backend can be obtained from matplotlib as matplotlib.get_backend() instead.

@Carreau Carreau added this to the 6.0 milestone Feb 7, 2017
@takluyver
Copy link
Member

@tacaswell can you comment on what we should be doing here? The suggested solution appears to be equivalent to what the comment just above that line says we're deliberately not doing, but I don't really understand the implications.

@tacaswell
Copy link
Contributor

This should probably be handled on the mpl side where we consume the env variables.

@jasongrout
Copy link
Member

@anntzer posted this to gitter:

I tried

 diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py
index 84d98a2da..c7981b3a1 100644
--- a/lib/matplotlib/__init__.py
+++ b/lib/matplotlib/__init__.py
@@ -1413,6 +1413,8 @@ try:
     use(os.environ['MPLBACKEND'])
 except KeyError:
     pass
+else:
+    rcParamsOrig['backend'] = os.environ['MPLBACKEND']


 def get_backend():

which would be the obvious solution. IPython appears to select the right backend when calling %matplotlib, but the raised window freezes, which suggests to me that something is wrong with the event loop in that case

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