The pgf output does not support a stream as illustrated by this snippet
ValueError: saving pgf to a stream is not supported, consider using the pdf option of the pgf-backend
Other formats like png, eps, ps, svg support it. I don't see any good reason to do not support streams for pgf and I guess it is a bug :)
Sorry, not a bug. Figures can contain images. If you want to write the output to a stream these must be embedded in your format, but there is no way to embed binary data like raster graphics in Latex code. The backend must write such an output to possibly multiple files.
Once compiled to PDF the whole figure can be represented by a single file, which is why the PDF output of the PGF backend on the other hand does support streams.
I'm going to close the issue since it is not a bug, but feel free to continue if you have further questions.
Wouldn't it suffice to raise the error, when there is actual binary data to handle?
This would be an option, but I didn't want users to think that pgf-to-stream is a general viable output method. I think for a toolkit like matplotlib imshow() isn't any less important than plot(). If I can't support such a core function, I'd rather guide the user to other output paths. As a user, I'd be pretty mad if I created some workflow that seems to work perfectly, only to learn some day that it's useless once trying to plot a simple image.
How about a "force" parameter then?
Hm, I did some research on this matter. It seems that LuaTeX provides the scripting tools necessary for writing binary files (e.g. png images) from embedded data. One could use a small Lua script that exports a base64 encoded image when the tex code is interpreted, which is then loaded with \includegraphics.
By the way, do you mind elaborating the use case of direct printing pgf code to a stream? Why not saving to a temporary file to stream from? It's not like you can get away with zero disk-access with latex in the pipeline anyway.
@pwuertz In my case, my workflow uses automation tools to build figures. It's very easy to use a stream and dealing with a file is much much more complicated and need a large refactoring. So, i'm convinced some users may prefer/need a stream.
@sciunto What about the pdf output of backend-pgf?
@pwuertz This is not sufficient for my usage. I really need the source code.
Me too, I use pythontex to embed matplotlib-generated graphs directly in my latex document.
@filmor But couldn't your embedded python code just save the figure to a file and return the appropriate \input or \includegraphics command?
For embedding the data for raster images the luatex scripting trick seems to work. When compiling the document the images will be written to files by Lua so they can be included by TeX as usual. I'm not really convinced of the benefit though, since in the end the binary data must be stored to separate files anyway + it increases latex code size and compile time.
Nevertheless, I guess for making your use cases work we could just downgrade that exception to a warning. I'll prepare a pull request for this soon.
Partial stream support is now in master.