Matplotlib:删除有关matplotlib.use()的警告

Ric*_*son 8 python warnings matplotlib

在我使用的Python模块中matplotlib,我想确保它在我通过远程计算机运行脚本时也能正常工作ssh.所以我这样做:

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
Run Code Online (Sandbox Code Playgroud)

有用.太糟糕了,当我直接在机器上运行它(不是远程机器!)时,它给了我以下警告:

对matplotlib.use()的这个调用没有效果,因为已经选择了后端; 必须 在首次导入pylab,matplotlib.pyplot或matplotlib.backends 之前调用matplotlib.use().

如何删除此邮件?

Gre*_*reg 5

虽然我无法测试这个Ipython告诉我"人们可以设置warn = False以禁止警告."

资源:

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`.
Run Code Online (Sandbox Code Playgroud)

在文档中找到拼写错误总是很有趣.

  • @FightFireWithFire,类似于`matplotlib.use('pgf',warn = False)`,你可能需要为'pgf'选择一个不同的后端. (3认同)

zer*_*cog 5

警告消息通常很重要,我建议不要忽略。我在寻找使用 sphinx 构建文档的解决方案时发现了您的问题。我收到了类似的消息,以及警告的一些附加上下文:

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'],
Run Code Online (Sandbox Code Playgroud)

然后我在https://github.com/conchoecia/pauvre/issues/18找到了解决方案。进口订单如下:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)

在修复之前,我只对模块进行了以下导入

from mymod.utils import plotutils
import mymod.plots as plots
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)

我认为这个问题的导入订单导致了警告消息。但是,我无法根据您提供的信息重现您的警告。如果能从该警告中看到更多的内容就太好了。

经过与其他开发人员的更多讨论后,很明显我导入的 pyplot 位于该文件中,而它属于我需要使用 plt 的模块中。

了解渲染非常重要,您可以在 https://matplotlib.org/faq/usage_faq.html#what-is-a-backendhttps://matplotlib.org/api/matplotlib_configuration_api.html#matplotlib 获取更多信息。使用 请记住其他正在进行的代码可能会更改或默认后端名称。