【问题标题】:Matplotlib: remove warning about matplotlib.use()Matplotlib:删除有关 matplotlib.use() 的警告
【发布时间】:2013-04-16 18:20:52
【问题描述】:

在我使用matplotlib 的Python 模块中,我想确保当我通过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

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

对 matplotlib.use() 的调用无效,因为后端 已经被选中; matplotlib.use() 必须在之前调用 pylab、matplotlib.pyplot 或 matplotlib.backends 是为 第一次。

如何删除此消息?

【问题讨论】:

    标签: python matplotlib warnings


    【解决方案1】:

    虽然我无法测试这个 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`.
    

    在文档中找到错字总是很有趣。

    【讨论】:

    • @Greg 我有完全相同的情况,错误只出现在本地机器上。 warn=False 删除错误,但脚本在执行前退出。相比之下,远程机器运行整个脚本。有任何想法吗?设置 - 带有 Python 3.6.0 的 Mac,带有 Python 3.4.2 的远程 Raspberry Pi 3
    • @SpencerH。恐怕我帮不了你 - 听起来你需要问一个新问题 - 祝你好运。
    • 请仔细考虑消音警告,因为一旦消音,即使是合法的,它们也永远不会再被看到。在我们的代码中,我们改为测试当前后端是否不是我们所期望的,然后才进行更改。
    • 我不清楚如何实现这个来抑制这个警告?
    • @FightFireWithFire,类似于matplotlib.use('pgf', warn=False),您可能需要选择不同的后端来“pgf”。
    【解决方案2】:

    警告信息通常很重要,我建议不要忽略。我在寻找使用 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'],
    

    然后我在 https://github.com/conchoecia/pauvre/issues/18 找到了解决方案。导入顺序如下:

    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    

    在修复之前,我只有以下模块的导入

    from mymod.utils import plotutils
    import mymod.plots as plots
    import matplotlib.pyplot as plt
    

    我认为这个问题的导入顺序导致了警告消息。但是,我无法针对所提供的信息重新创建您的警告。如果能从该警告中再看到几行,那就太好了。

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

    了解渲染很重要,您可以在 https://matplotlib.org/faq/usage_faq.html#what-is-a-backendhttps://matplotlib.org/api/matplotlib_configuration_api.html#matplotlib.use 请记住,其他处理代码可能会更改或默认后端名称。

    【讨论】:

    • 额外的上下文信息似乎是必须知道的。我想这是一个相对较新的 matplotlib 功能?我似乎没有在旧版本中得到它,如果没有它,就不可能知道对“使用”的冒犯性调用在哪里......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2022-09-30
    • 2021-08-18
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多