[Matplotlib-users] Documentation help request for web frameworks
Joseph Fox-Rabinovitz
jfoxrabinovitz at gmail.com
Tue Sep 6 11:26:12 EDT 2016
I don't have the time to do a decent writeup of this, but here is a
function that I use that does the byteio thing automatically. Perhaps it
will be useful to someone who is willing to do the writeup. I trimmed out
the domain-specific parts and left only the interesting bits:
```
def saveFigure(fig, file=None, **kwargs):
"""
Saves the figure as an image using `matplotlib.pyplot.savefig`.
The main value of this method is that it automatically saves to memory
via
a `BytesIO` object if a file is not specified.
fig: The figure to save
file: Either a file name or file-like object, or `None`. In the former
two
cases, there is no return value. In the latter case, a `BytesIO`
containing the image will be returned. The output will be rewound to
the start in that case. The default is `None`.
All other arguments are passed through directly to `savefig`. `format`
defaults
to 'png' if unset. Some common options include:
"""
output = BytesIO() if file is None else file
kwargs.setdefault('format', 'png')
fig.savefig(output, **kwargs)
if file is None:
output.seek(0)
return output
```
On Mon, Sep 5, 2016 at 2:28 PM, Thomas Caswell <tcaswell at gmail.com> wrote:
> Folks,
>
> I know that there is a use of mpl as part of django / flask / bottle /
> zope / ... to write figures directly to buffers and serve them to users
> without writing to disk (an example of this is http://scipy-cookbook.
> readthedocs.io/items/Matplotlib_Django.html , but I think this is better
> done via `fig.savefig(bytes_io_buffer, format='png')` rather than reaching
> in and touching the canvas directly).
>
> This seems like the sort of thing that should be easy to find in our
> documentation. Is anyone interested in writing up 'canonical' examples?
>
> Tom
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160906/8b8e16c5/attachment.html>
More information about the Matplotlib-users
mailing list