在脚本中使用Matplotlib的渲染器问题

Dav*_*ave 6 python matplotlib

我已经缩小到这个电话:

fig.canvas.tostring_argb() #fig=matplotlib.pyplot.figure()
Run Code Online (Sandbox Code Playgroud)

AttributeError当我将代码作为python脚本运行时,此函数引发了一个问题. AttributeError: 'FigureCanvasGTKAgg' object has no attribute 'renderer'

但是,如果在ipython --pylab命令行中运行,此代码可以正常工作.

据我所知,文档中的Agg渲染器应该可以正常工作.

上下文是我试图从数字制作电影,而不是将帧保存到磁盘; 按照这个问题.我正在使用流式传输像素数组的方法ffmpeg(作为一个单独的进程运行)来执行此操作,我需要argb帧中的值数组.

我是否可以通过一些配置设置让matplotlib在脚本中正常工作?

编辑use('Agg')根据评论 尝试; 仍然失败; 这是一个最小的工作示例.

[dave@dave tools]$ python -c "import matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot; fig=matplotlib.pyplot.figure(); fig.canvas.tostring_argb()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 416, in tostring_argb
    return self.renderer.tostring_argb()
AttributeError: FigureCanvasAgg instance has no attribute 'renderer'
Run Code Online (Sandbox Code Playgroud)

小智 15

我怀疑你错过了这个电话:

fig.canvas.draw()
Run Code Online (Sandbox Code Playgroud)

之前

fig.canvas.tostring_argb()
Run Code Online (Sandbox Code Playgroud)

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.tostring_argb()
Run Code Online (Sandbox Code Playgroud)

对我失败了,但是

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.draw()
fig.canvas.tostring_argb()
Run Code Online (Sandbox Code Playgroud)

作品.


Dav*_*ave 3

我最终安装并使用了WXAgg后端;默认AggGTKAgg,对我来说不起作用。