This call to matplotlib.use() has no effect

8,127 views
Skip to first unread message

ben h

unread,
Mar 18, 2012, 11:15:16 PM3/18/12
to spyd...@googlegroups.com
I want to set the matplotlib backend from within my script. I can do this if I run my script from a shell/terminal, but from Spyder I get an error. 
Here is the full error message:


/usr/local/lib/python2.6/dist-packages/matplotlib-1.0.1-py2.6-linux-x86_64.egg/matplotlib/__init__.py:888: UserWarning: This call to matplotlib.use() has no effect

because the the backend has already been chosen;

matplotlib.use() must be called *before* pylab, matplotlib.pyplot,

or matplotlib.backends is imported for the first time.

if warn: warnings.warn(_use_error_msg)


I searched this list and found this old message that seems related:
https://groups.google.com/d/msg/spyderlib/rAWkNkGWpK0/1fII0P5leowJ


Here is a snippet from my script:

#!/usr/bin/env python

"""
    Bore data plotting routine
"""
# if needed set the backend before importing pyplot:
import matplotlib
matplotlib.use("PS")

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc

... commands ...

David Verelst

unread,
Mar 19, 2012, 5:26:19 AM3/19/12
to spyd...@googlegroups.com
Hi,

The Spyder consoles start already with Matplotlib (and pyplot), NumPy and SciPy imported. This means that you can't change the Matplotlib backend anymore since pyplot/pylab module is already imported when the console starts. Remember that you have to set the Matplotlib backend before importing pyplot. This also explains why in a stand alone python console, one not connected to spyder, does work as you expected: there's not a single external module imported upon startup.

To change the Matplotlib backend Spyder uses:
Preferences > Console > External modules > Matplotlib GUI backend

To my understanding (note that I am not a developer), these imports are done in order to get some of Spyders other features to work (e.g. monitor). I assume that, theoretically, you could disable the Matplotlib/pyplot imports upon the startup of a console, but I am not sure where exactly this should be done in the code, and what other consequences this will have.

Maybe a note on Matplotlib backends should be added to the documentation (for instance in the Console section)? I don't mind drafting a proposed change for this somewhere this week.

Regards,
David
--
You received this message because you are subscribed to the Google Groups "spyder" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spyderlib/-/tH5mr59C11MJ.
To post to this group, send email to spyd...@googlegroups.com.
To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spyderlib?hl=en.

Pierre Raybaut

unread,
Mar 19, 2012, 5:36:27 PM3/19/12
to spyd...@googlegroups.com
Hi all,

If you are executing your script in a dedicated Python interpreter
(default behavior), then Matplotlib will be imported before executing
your script only if the backend option (mentioned by David) is checked
(default value). So, to avoid this, simply uncheck this option, or
choose the backend that you want there (@David: the monitor does not
require Matplotlib at startup).

If you are executing your script in an existing Python interpreter,
with default options, the PYTHONSTARTUP script is the
'scientific_startup.py' script which also imports Matplotlib. But this
can also be changed in Preferences.

Last but not least, I do agree with you David that this kind of
side-effect should be documented somewhere.

Ben, could you tell us where would you have expected this to be documented?

-Pierre

Ben Harrison

unread,
Apr 26, 2012, 10:59:54 PM4/26/12
to spyd...@googlegroups.com
On 20/03/12 08:36, Pierre Raybaut wrote:
> Hi all,
>
> If you are executing your script in a dedicated Python interpreter
> (default behavior), then Matplotlib will be imported before executing
> your script only if the backend option (mentioned by David) is checked
> (default value). So, to avoid this, simply uncheck this option, or
> choose the backend that you want there (@David: the monitor does not
> require Matplotlib at startup).
>
> If you are executing your script in an existing Python interpreter,
> with default options, the PYTHONSTARTUP script is the
> 'scientific_startup.py' script which also imports Matplotlib. But this
> can also be changed in Preferences.
>
> Last but not least, I do agree with you David that this kind of
> side-effect should be documented somewhere.
>
> Ben, could you tell us where would you have expected this to be documented?
>
> -Pierre

Apologies for late reply, I didn't see your message until now.

It's a tough one to call really - I wasn't familiar with the .use() call
at first, so had to check and re-check matplotlib's docs a number of
times to make sure I had syntax right, backends installed, etc etc.
Pretty frustrating until I ran the script from shell prompt and twigged.

I suppose anywhere in the help docs that's searchable, perhaps using as
many related terms as possible: backend, mpl.use(), pylab. Maybe even
quoting the warning message from matplotlib's __init__.py:

> /usr/lib/pymodules/python2.6/matplotlib/__init__.py:890: UserWarning: This call to matplotlib.use() has no effect
> because the the backend has already been chosen;
> matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
> or matplotlib.backends is imported for the first time.

Ben.

Reply all
Reply to author
Forward
0 new messages