1

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.