Error in Django when using matplotlib examples

前端 未结 2 1747
旧巷少年郎
旧巷少年郎 2020-12-11 02:58

I am testing several cases of Django and matplotlib such as this question or in french.

Each time, it works on my mac, but does not on my server, where I receive the

2条回答
  •  星月不相逢
    2020-12-11 03:28

    At the moment, matplotlib's writing functions require the seek ducktype to use the response at a file. You can write to a buffer, like this:

    import io
    
    def mplimage(request):
        f = matplotlib.figure.Figure()
    
        # Code that sets up figure goes here; in the question, that's ...
        FigureCanvasAgg(f)
    
        buf = io.BytesIO()
        plt.savefig(buf, format='png')
        plt.close(f)
        response = HttpResponse(buf.getvalue(), content_type='image/png')
        return response
    

提交回复
热议问题