matplotlib is great, particularly the image quality. I'm using matplotlib to generate images in a webapp and have run into a problem. How do I get it to give me the rendered image bytes in a string rather than writing the image to a file?
The docs for FigureCanvasAgg.print_figure() says that if the filename is actually a file object then it will write the bytes to that file object. This works great if I pass in sys.stdout, but as soon as I try to use a StringIO() instance to capture the bytes in Python I get an error:
File "/usr/lib/python2.3/site-packages/matplotlib/backends/backend_agg.py", line 453, in print_figure
TypeError: Could not convert object to file pointer
The same use of StringIO does work with GD; is this a bug in the Agg backend? Please see attached test code.
BTW, while I'm here, I noticed a discusison on matplotlib-devel about how to specify the image format when rendering it to the backend:
I'm in favour of an explicit format argument as well, for exactly the webapp use case. I'd like to easily be able to get the bytes for either the PNG or SVG of an image.
aggStringIO.txt (843 Bytes)