Create a gist now

Instantly share code, notes, and snippets.

Example of rendering a matplotlib image directly to Flask view
from flask import Flask, make_response
app = Flask(__name__)
def simple():
import datetime
import StringIO
import random
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.dates import DateFormatter
for i in range(10):
y.append(random.randint(0, 1000))
ax.plot_date(x, y, '-')
png_output = StringIO.StringIO()
response.headers['Content-Type'] = 'image/png'
return response
if __name__ == "__main__":
jyoo commented Jan 18, 2012

Yo this is awesome. Thank you and works perfectly out of the box.

Wayn0 commented Nov 20, 2013

Thanks, this has given me some ideas!


Thanks! I teaching my stats class using Python and this is very helpful.


Just what I was looking for. Thanks!


I have a problem that when render matplot image directly into flask view , I loose my basic features of matplot image(like now it donot show coordinates when hovering over the image ,now I cannot zoom into a particular portion of image etc ) . Can you suggest how can I render matplotimage into flask and still having these features of images ? (I am able to do that using pygal but not matplot lib)
Thanks in advance :)


Thanks Andy. :)


Great! thanks that works wonderfully.
Is there a way that I can do something like that and using Drawnow() and plotting a PNG image that will show my graph and also interactive mode.
I was trying to figure that ourt and I am getting always Error with $DISPLAY.


thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment