Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renderer problems using Matplotlib from within a script

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'
like image 352
Dave Avatar asked Nov 18 '13 15:11

Dave


People also ask

Why is matplotlib not working?

Occasionally, problems with Matplotlib can be solved with a clean installation of the package. In order to fully remove an installed Matplotlib: Delete the caches from your Matplotlib configuration directory. Delete any Matplotlib directories or eggs from your installation directory.

What is matplotlib use (' AGG ')?

The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.

What is the difference between matplotlib and matplotlib Pyplot?

Pyplot is an API (Application Programming Interface) for Python's matplotlib that effectively makes matplotlib a viable open source alternative to MATLAB. Matplotlib is a library for data visualization, typically in the form of plots, graphs and charts.

What is matplotlib renderer?

the matplotlib.backend_bases.Renderer is the object which knows how to draw on the FigureCanvas. and the matplotlib.


2 Answers

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.

like image 155
Matthew Shun-Shin Avatar answered Oct 28 '22 22:10

Matthew Shun-Shin


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

like image 22
Dave Avatar answered Oct 28 '22 21:10

Dave