You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Write canonical example of how to use Matplotlib inside a webserver #12628
As shown by #11094#12612 matplotlib is used inside of webservers to dynamically serve plots. The example should show:
importance of picking a non-GUI backend (both by setting the backend and explicitly by not using pyplot). Given that these servers are multi-threaded, not using pyplot at all may be the best option to avoid any coupling between workers via pyplot and memory leaks due to pyplot holding references to figures forever. Should probably show use of new_figure_manager
saving to a bytesIO buffer to avoid disk
png, svg, and pdf
be applicable to both django and flask (not sure off the top of my head what the differences are), bonus points for cherrypy, aiohttp, tornado, and pyramid
Rather than bothering with any explicit backend at all, I would strongly suggest merging #12450 and have the canonical approach be
from matplotlib.figure import Figure
fig = Figure()
<do the plotting>
fig.savefig(...)
Even with 3.0 (i.e. without that PR) new_figure_manager is totally overkill, just manually attaching a canvas (FigureCanvasAgg(fig) or even just FigureCanvasBase(fig)) is enough.
(... except if you want to use a third-party renderer e.g. mplcairo, but I doubt that's the point of the proposed example)
import base64
from io import BytesIO
from flask import Flask
from matplotlib.figure import Figure
app = Flask(__name__)
@app.route("/")
def hello():
# Generate the figure **without using pyplot**.
fig = Figure()
ax = fig.subplots()
ax.plot([1, 2])
# Save it to a temporary buffer.
buf = BytesIO()
fig.savefig(buf, format="png")
# Embed the result in the html output.
data = base64.b64encode(buf.getbuffer()).decode("ascii")
return f"<img src='data:image/png;base64,{data}'/>"
I prefer suggesting the use of savefig (and yes you can also pass the Response buffer to it), as it's a regular method that you'd also use in other contents, rather than print_figure, which is a bit an implementation detail.
Hi problem goes away really nicely using the AGG backend
I just import matlplotlib at the top of my code and follow this with matplotlib.use('AGG') before importing any other matplotlib components (matplotlib.pyplot in this case)
No need for the matplotlib GUI backend in the web server.
Running on a Mac(High Sierra) with a Django server
Activity
anntzer commentedon Oct 25, 2018
Rather than bothering with any explicit backend at all, I would strongly suggest merging #12450 and have the canonical approach be
Even with 3.0 (i.e. without that PR) new_figure_manager is totally overkill, just manually attaching a canvas (
FigureCanvasAgg(fig)or even justFigureCanvasBase(fig)) is enough.(... except if you want to use a third-party renderer e.g. mplcairo, but I doubt that's the point of the proposed example)
mgeier commentedon Nov 3, 2018
Possible duplicate: #4124.
tacaswell commentedon Nov 3, 2018
Indeed! I'm going to close the older one.
anntzer commentedon Nov 3, 2018
Do you want to make a decision about #12450, by the way? :) (as noted above)
anntzer commentedon Nov 27, 2018
@tacaswell I think something like
is what you're looking for?
story645 commentedon Nov 27, 2018
My version was (stolen from a cookbook)
where the outfile is the responsebuffer, like
anntzer commentedon Nov 27, 2018
canvas = FigureCanvas(fig)is not necessary anymore since Attach a FigureCanvasBase by default to Figures. #12450 (that's the whole point of that PR).savefig(and yes you can also pass the Response buffer to it), as it's a regular method that you'd also use in other contents, rather thanprint_figure, which is a bit an implementation detail.tacaswell commentedon Dec 1, 2018
Yes, something as simple as that is what I had in mind.
Agree on preferring
savefigoverprint_figure.pjmnoble commentedon Oct 2, 2019
Hi problem goes away really nicely using the AGG backend
I just import matlplotlib at the top of my code and follow this with matplotlib.use('AGG') before importing any other matplotlib components (matplotlib.pyplot in this case)
No need for the matplotlib GUI backend in the web server.
Running on a Mac(High Sierra) with a Django server