首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matplotlib:删除有关matplotlib.use()的警告

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

Stack Overflow用户
提问于 2013-04-23 13:45:54
回答 2查看 15.2K关注 0票数 8

在使用matplotlib的Python模块中,当我通过ssh在远程计算机上运行脚本时,我希望确保它也能工作。所以我想:

代码语言:javascript
复制
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

它起作用了。糟糕的是,当我在机器上直接运行(而不是远程!)时,它给了我以下警告:

对matplotlib.use()的调用没有任何效果,因为后端已经被选中;必须在首次导入pylab、matplotlib.pyplot或matplotlib.backends之前调用matplotlib.use()。

如何删除此消息?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-23 14:11:40

虽然我无法测试,但这个Ipython告诉我,“可以设置warn=False来消除警告。”

来源:

代码语言:javascript
复制
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`.

在文档中找到一个错误总是很有趣的。

票数 5
EN

Stack Overflow用户

发布于 2019-01-15 20:39:26

警告消息通常是重要的,我建议不要忽略。我发现你的问题时,寻找解决方案,我的医生建设与斯芬克斯。我收到了一条类似的消息,并收到了一些警告的附加上下文:

代码语言:javascript
复制
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'],

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

代码语言:javascript
复制
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

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

代码语言:javascript
复制
from mymod.utils import plotutils
import mymod.plots as plots
import matplotlib.pyplot as plt

我认为这个问题的进口订单产生了警告信息。但是,我无法为所提供的信息重新创建您的警告。如果能从这个警告中看到更多的台词,那就太好了。

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

理解呈现很重要,您可以在faq.html#what-is-a-backendapi.html#matplotlib.use获得更多信息--只需记住其他过程代码可能正在更改或默认后端名称。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16170989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档