Reputation: 75
So I don't want to use plt.savefig and want to render my graph dynamically. Please look into this, it is saving the graph and then rendering -
def get_graph(pressure, flow, fr_value):
fig = plt.figure()
img=io.BytesIO()
plt.plot(flow, pressure)
plt.savefig(img,format='png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode()
return plot_url
and my flask app -
plot_url = graph.get_graph(int(pressure), int(flow), fr_value) # figure from get_graph url
return render_template('plot.html', url=plot_url)
html -
<body>
<img src="data:image/png;base64,{{url}}" alt="Chart" height="auto" width="60%">
</body>
Upvotes: 1
Views: 1189
Reputation: 24966
One of the non-obvious problems I ran into when trying to do that was discovering the need for
import matplotlib
matplotlib.use('agg')
so that matplotlib wouldn't try to use tkinker. Try adding that.
An alternative approach is to provide a separate method to render the image. I have a working example of that here that you're welcome to borrow code from.
Upvotes: 1