2013-04-23 37 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