今天在把电脑上的python程序复制到树莓派的时候发现树莓派无法正常运行,报错代码如下:
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都可以解决问题,你可以用任意一个。至于区别我也不是很清楚。
原因如下:
#这是官网解释
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.