如何通过html在烧瓶中显示matplotlib图(不保存图像)

时间:2020-07-02 17:22:38

标签: python html matplotlib flask

因此,我不想使用plt.savefig并希望动态呈现图形。请调查一下,它正在保存图形,然后呈现-

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

和我的烧瓶应用程序-

    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 个答案:

答案 0 :(得分:1)

当我尝试这样做时遇到的一个非显而易见的问题是发现需要

import matplotlib
matplotlib.use('agg')

以便matplotlib不会尝试使用tkinker。尝试添加。

另一种方法是提供一种单独的方法来渲染图像。我有一个here的工作示例,欢迎您从中借用代码。