我发现matplotlib
仅使用将图表绘制到文件中并不像阅读教程看起来那么容易。在教程中解释说,您只需累积数据,然后:
import matplotlib.pyplot as plt
# ... fill variables with meaningful stuff
plt.plot(data.x,data.y,format_string)
plt.savefig(filename)
并做了。如果您只是将其作为 shell 执行,那也可以正常工作。但是,如果您将此代码提供给没有任何窗口的进程(如 jenkins),那么您只会收到以下错误:
Traceback (most recent call last):
File "./to_graph.py", line 89, in <module>
main()
File "./to_graph.py", line 78, in main
plt.plot(warnings[0],warnings[1],args.format_warnings,label="Warnings")
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2460, in plot
ax = gca()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 701, in gca
ax = gcf().gca(**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 369, in gcf
return figure()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
据我了解堆栈跟踪行周围的源代码,此错误的原因是 backend_tkagg.py 期望使用现有窗口(请参阅堆栈跟踪中的 Tk.Tk() 行)。所以我想知道是否有办法用matplotlib(或python)绘制图表而不依赖于窗口(-manager)来完成工作。