Passing a matplotlib figure to HTML (flask)

前端 未结 5 1642
遥遥无期
遥遥无期 2020-11-29 20:41

I am using matplotlib to render some figure in a web app. I\'ve used fig.savefig() before when I\'m just running scripts. However, I need a function to return a

5条回答
  •  迷失自我
    2020-11-29 21:16

    I am working with Python 3.x, I have changed some lines of the code and it worked for me. I had the following error message: ".....object has no attribute 'savefig'"

    @app.route('/fig/')
    
    def fig(cropzonekey):
        #fig = draw_polygons(cropzonekey)
        fig = plt.plot([1,2,3,4], [1,2,3,4])
        #img = StringIO()
        img = BytesIO()
        #fig.savefig(img)
        plt.savefig(img)
        img.seek(0)
        return send_file(img, mimetype='image/png')
    

提交回复
热议问题