2013-04-23 38 views
4

在我使用matplotlib的Python模块中,我想确保它在我通过ssh在远程计算机上运行脚本时也能正常工作。所以我这样做:Matplotlib:删除有关matplotlib.use()的警告

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第一次为 导入。

我该如何删除此消息?

回答

3

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

总是有趣的发现在文档一个错字。

+0

不错!有用! – 2013-04-23 15:19:48

+0

@Greg我有完全相同的情况,只在本地机器上显示错误。 'warn = False'消除错误,但脚本在执行前退出。相比之下,远程机器运行整个脚本。有任何想法吗?设置 - Mac与Python 3.6.0,远程树莓派3与Python 3.4.2 – 2017-02-08 15:16:24

+0

@SpencerH。恐怕我无法帮助你 - 这听起来像你需要问一个新问题 - 祝你好运。 – Greg 2017-02-09 10:54:35