linux终端使用python的matplotlib模块画图出现“could not open display”问题解决

转载地址:http://blog.csdn.net/together_cz/article/details/70195374

在数据挖掘、数据分析领域里面,经常需要对处理后得到的数据进行可视化的呈现,这是一种更为直观、更为清晰的表达方式,让接受者可以更加直观的把握整体数据的分布或者走向等信息。在linux系统下使用python的matplotlib模块来画图出现一个问题如下:

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 97, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
    globals(),locals(),[backend_name])
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
    from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 13, in <module>
    import gtk; gdk = gtk.gdk
  File "/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
    _init()
  File "/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
    _gtk.init_check()
RuntimeError: could not open display


这是display错误,之前的解决办法是在网上查资料得到的,使用的是Xmanger这个小软件,成功了连接了本地和虚拟机,可以在虚拟机终端的形式下输出图片,也可以保存、展示,但是不知道为什么,最近再次使用这个matplotlib模块画图的时候出现同样的错误,Xmanger也不好使了,暂时还是不知道怎么回事,没有办法只好另寻出路了

    记得之前查资料的时候有一个解决方案使用的是添加一行代码的形式,忘记了添加的是什么了索性直接查一下资料,得到如下的解决方法:

>>> import matplotlib as mpl
>>> mpl.use('Agg')
>>> import matplotlib.pyplot as plt
试验一下,果然奏效,简单来画一幅图片:linux终端使用python的matplotlib模块画图出现“could not open display”问题解决linux终端使用python的matplotlib模块画图出现“could not open display”问题解决
  1. #!/usr/bin/env python  
  2. #coding:utf-8  
  3.   
  4. import matplotlib as mpl  
  5. mpl.use('Agg')  
  6. import numpy as np  
  7. import matplotlib.pyplot as plt  
  8. t = np.arange(-12, .01)  
  9. s = np.sin(2 * np.pi * t)  
  10.   
  11. plt.plot(t,s)  
  12. # draw a thick red hline at y=0 that spans the xrange  
  13. l = plt.axhline(linewidth=4, color='r')  
  14. plt.axis([-12, -12])  
  15. plt.show()  
  16. plt.close()  
  17.   
  18. # draw a default hline at y=1 that spans the xrange  
  19. plt.plot(t,s)  
  20. l = plt.axhline(y=1, color='b')  
  21. plt.axis([-12, -12])  
  22. plt.show()  
  23. plt.close()  
  24.   
  25. # draw a thick blue vline at x=0 that spans the upper quadrant of the yrange  
  26. plt.plot(t,s)  
  27. l = plt.axvline(x=0, ymin=0, linewidth=4, color='b')  
  28. plt.axis([-12, -12])  
  29. plt.show()  
  30. plt.close()  
  31.   
  32. # draw a default hline at y=.5 that spans the the middle half of the axes  
  33. plt.plot(t,s)  
  34. l = plt.axhline(y=.5, xmin=0.25, xmax=0.75)  
  35. plt.axis([-12, -12])  
  36. plt.show()  
  37. plt.close()  
  38.   
  39. plt.plot(t,s)  
  40. p = plt.axhspan(0.250.75, facecolor='0.5', alpha=0.5)  
  41. p = plt.axvspan(1.251.55, facecolor='g', alpha=0.5)  
  42. plt.axis([-12, -12])  
  43. plt.savefig('a.png')  
  44. plt.show()  


linux终端使用python的matplotlib模块画图出现“could not open display”问题解决

代码来源于:http://blog.csdn.net/pipisorry/article/details/40005163