Matplotlib:删除关于 matplotlib.use() 的警告 [英] Matplotlib: remove warning about matplotlib.use()
问题描述
在我使用 matplotlib
的 Python 模块中,我想确保当我通过 ssh
在远程机器上运行脚本时它也能正常工作.所以我这样做:
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:
这个对 matplotlib.use() 的调用没有效果,因为后端已经被选中;matplotlib.use() 必须在之前调用pylab、matplotlib.pyplot 或 matplotlib.backends 是为第一次.
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.
如何删除此消息?
推荐答案
虽然我无法测试这个,但 Ipython 告诉我可以设置 warn=False 来支持警告."
While I can't test this Ipython tells me that "one can set warn=False to supporess the warnings."
来源:
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.
这篇关于Matplotlib:删除关于 matplotlib.use() 的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!