今天在把電腦上的python程序復(fù)制到樹莓派的時候發(fā)現(xiàn)樹莓派無法正常運行,報錯代碼如下:
File "/usr/local/lib/python3.4/dist-packages/matplotlib/pyplot.py", line 114, 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 11, in <module>
from . import backend_gtk3
File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_gtk3.py", line 58, in <module>
cursors.MOVE : Gdk.Cursor.new(Gdk.CursorType.FLEUR),
TypeError: constructor returned NULL
解決辦法是在程序import matplotlib.pyplot as plt 前面增加以下兩句
import matplotlib
#matplotlib.use("Agg")
matplotlib.use("Pdf")
import matplotlib.pyplot as plt
上面的Agg,Pdf都可以解決問題,你可以用任意一個。至于區(qū)別我也不是很清楚。
原因如下:
#這是官網(wǎng)解釋
If you use the use() function, this must be done before importing matplotlib.pyplot.
Calling use() after pyplot has been imported will have no effect.
Using use() will require changes in your code if users want to use a different backend.
Therefore, you should avoid explicitly calling use() unless absolutely necessary.