Matplotlib: remove warning about matplotlib.use()

matplotlib, python, warnings

Solution

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.

Problem

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?

Original source