15

I've narrowed down to this call:

fig.canvas.tostring_argb() #fig=matplotlib.pyplot.figure()

this function raises an AttributeError when I run the code as a python script. AttributeError: 'FigureCanvasGTKAgg' object has no attribute 'renderer'

However, this code works properly if run in the ipython --pylab command line.

As far as I can tell from the documentation, the Agg renderer should work OK.

The context is that I'm trying to make a movie from figures, without saving the frames to disk; as per this question. I'm using the approach that streams the pixel arrays to ffmpeg (running as a separate process) to do this, I need the argb array of values from the frame.

Is there some configuration setting I can make to get matplotlib to work correctly from within a script?

Edit Tried use('Agg') as per a comment; still fails; this is a minimal working example.

[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'
Community
  • 1
  • 1
Dave
  • 7,555
  • 8
  • 46
  • 88
  • 3
    put `matplotlib.use('Agg')` at the top of your script. The issue is that you are trying to use a gui backend with out properly setting it up. It would be better if you could generate a 10 line script which demonstrates your issue. – tacaswell Nov 18 '13 at 16:15

2 Answers2

35

I suspect that you have missed out the call to:

fig.canvas.draw()

before

fig.canvas.tostring_argb()

as

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.tostring_argb()

fails for me, but

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.draw()
fig.canvas.tostring_argb()

works.

3

I ended up installing and using the WXAgg backend; the Agg,and default GTKAgg, didn't work for me.

Dave
  • 7,555
  • 8
  • 46
  • 88