In a Python module where I use matplotlib
, I want to make sure it works also when I run the script on a remote machine via ssh
. So I do:
import matplotlib
matplotlib.use('Agg')
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import numpy as np
import pylab
import scipy.stats
import scipy.stats.mstats
It works. Too bad that when I run it directly on a machine (not a remote one!), it gives me the following warning:
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.
How do I remove this message?
While I can't test this Ipython tells me that "one can set warn=False to supporess the warnings."
Source:
matplotlib.use?
Type: function
String Form:<function use at 0x98da02c>
File: /usr/lib/pymodules/python2.7/matplotlib/__init__.py
Definition: matplotlib.use(arg, warn=True)
Docstring:
Set the matplotlib backend to one of the known backends.
The argument is case-insensitive. For the Cairo backend,
the argument can have an extension to indicate the type of
output. Example:
use('cairo.pdf')
will specify a default of pdf output generated by Cairo.
.. note::
This function must be called *before* importing pyplot for
the first time; or, if you are not using pyplot, it must be called
before importing matplotlib.backends. If warn is True, a warning
is issued if you try and call this after pylab or pyplot have been
loaded. In certain black magic use cases, e.g.
:func:`pyplot.switch_backends`, we are doing the reloading necessary to
make the backend switch work (in some cases, e.g. pure image
backends) so one can set warn=False to supporess the warnings.
To find out which backend is currently set, see
:func:`matplotlib.get_backend`.
Always fun to find a typo in the docs.
Warning messages are usually significant, and I recommend not ignoring. I found your question while searching for a solution to my doc build with sphinx. I received a similar message, and some additional context for the warning:
UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called before pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
The backend was originally set to 'Qt5Agg' by the following code:
File "setup.py", line 131, in <module>
'psql' : ['psycopg2>=2.7.1'],
I then found a solution at https://github.com/conchoecia/pauvre/issues/18 . With the import order as follows:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
Before the fix I only had the following import for the module
from mymod.utils import plotutils
import mymod.plots as plots
import matplotlib.pyplot as plt
I am thinking the import order for this question resulted in the warning message. However, I was not able to recreate your warning for the information provided. It would have been nice to see a couple more lines from that warning.
After some more discussions with other developers, it became apparent my import of pyplot was in the file whereas it belongs in the module just where I need to use plt.
Understanding the render is important, and you can get more at https://matplotlib.org/faq/usage_faq.html#what-is-a-backend and https://matplotlib.org/api/matplotlib_configuration_api.html#matplotlib.use Just remember other proceeding code may be changing or defaulting the backend names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With