【问题标题】:how to show matplotlib graph in flask through html (Without saving image)如何通过html在flask中显示matplotlib图(不保存图像)
【发布时间】:2020-07-03 01:37:19
【问题描述】:

所以我不想使用 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>

【问题讨论】:

    标签: python html matplotlib flask


    【解决方案1】:

    我在尝试这样做时遇到的一个不明显的问题是发现需要

    import matplotlib
    matplotlib.use('agg')
    

    这样 matplotlib 就不会尝试使用 tkinker。尝试添加它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 2015-05-21
      • 1970-01-01
      • 2019-09-19
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多