2017-05-30 148 views
2

無法導入matplotlib.pyplot。已經將我的pi升級到Jessie,Python3.4,從源碼安裝了matplotlib。 下面是一些代碼,我從這個網站獲得和修改,以嘗試和調試:不能「導入matplotlib.pyplot爲plt」

#!/usr/bin/python3.4 
#find_backends.py 
#from pylab import * 
import time 

import matplotlib as mpl 
import matplotlib.backends 
#import matplotlib.pyplot as p 
import os.path 


def is_backend_module(fname): 
    """Identifies if a filename is a matplotlib backend module""" 
    return fname.startswith('backend_') and fname.endswith('.py') 

def backend_fname_formatter(fname): 
    """Removes the extension of the given filename, then takes away the leading 'backend_'.""" 
    return os.path.splitext(fname)[0][8:] 

print("thebackend= " + mpl.get_backend()) 
# get the directory where the backends live 
backends_dir = os.path.dirname(matplotlib.backends.__file__) 
print("backends_dir: \t" + str(backends_dir)) 

# filter all files in that directory to identify all files which provide a backend 
backend_fnames = filter(is_backend_module, os.listdir(backends_dir)) 

backends = [backend_fname_formatter(fname) for fname in backend_fnames] 

print("supported backends: \t" + str(backends)) 

# validate backends 
backends_valid = [] 
for b in backends: 
    try: 
     p.switch_backend(b) 
     backends_valid += [b] 
    except: 
     continue 

print("valid backends: \t" + str(backends_valid)) 


# try backends performance 
for b in backends_valid: 

    ion() 
    try: 
     p.switch_backend(b) 


     clf() 
     tstart = time.time()    # for profiling 
     x = arange(0,2*pi,0.01)   # x-array 
     line, = plot(x,sin(x)) 
     for i in arange(1,200): 
      line.set_ydata(sin(x+i/10.0)) # update the data 
      draw()       # redraw the canvas 

     print(b + ' FPS: \t' , 200/(time.time()-tstart)) 
     ioff() 

    except: 
     print(b + " error :(") 

結果:

thebackend= GTK3Agg 
backends_dir: /usr/local/lib/python3.4/dist-packages/matplotlib/backends 
supported backends:  ['agg', 'qt5', 'mixed', 'template', 'cairo', 'gtkcairo', 'gtk3', 'webagg_core', 'gtk3cairo', 'pdf', 'gdk', 'ps', 'wx', 'wxagg', 'qt5agg', 'macosx', 'qt4agg', 'qt4', 'nbagg', 'gtkagg', 'tkagg', 'pgf', 'webagg', 'svg', 'gtk3agg', 'gtk'] 
valid backends:  [] 


------------------ 
(program exited with code: 0) 
Press return to continue 

因此,後端是GTK3AGG?爲什麼大寫字母不匹配支持的後端列表? 如果我從#進口matplotlib.pyplot刪除#爲p導入將失敗,我也得到:

** (find_backends.py:1057): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files 
Traceback (most recent call last): 
    File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_cairo.py", line 33, in <module> 
    import cairocffi as cairo 
ImportError: No module named 'cairocffi' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_cairo.py", line 36, in <module> 
    import cairo 
ImportError: No module named 'cairo' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "find_backends.py", line 8, in <module> 
    import matplotlib.pyplot as p 
    File "/usr/local/lib/python3.4/dist-packages/matplotlib/pyplot.py", line 115, in <module> 
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() 
    File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup 
    globals(),locals(),[backend_name],0) 
    File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 12, in <module> 
    from .backend_cairo import cairo, HAS_CAIRO_CFFI 
    File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_cairo.py", line 38, in <module> 
    raise ImportError("Cairo backend requires that cairocffi or pycairo is installed.") 
ImportError: Cairo backend requires that cairocffi or pycairo is installed. 


------------------ 
(program exited with code: 1) 
Press return to continue 

開羅被列爲支持的後端但在這裏失敗。我不確定接下來要嘗試什麼。我一直試圖從「Raspberry Pi Python程序員食譜」這本書中找到一個matplotlib的例子來開展工作,並且工作了大約2年。我幾乎準備好再次放棄。

+0

嘗試安裝'cairocffi'或'pycairo'? – DavidG

回答

1

答案在macplotlib.use()。正如它注意到的,你必須在matplotlib.pyplot導入之前使用use()。我不得不嘗試上面找到的幾個後彎來找到一個可行的。

import numpy as np 
import matplotlib as mpl 
mpl.use('tkagg') #YAAA!! this finally makes the Damn thing work 
import matplotlib.pyplot as plt