Plot matplotlib on the Web

前端 未结 2 1039
粉色の甜心
粉色の甜心 2020-12-13 16:08

The following code will of course create a PNG named test and save it on the server:

from matplotlib.figure import Figure                         
from matpl         


        
2条回答
  •  孤街浪徒
    2020-12-13 16:44

    Just to update for python3

    The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO https://docs.python.org/3.5/whatsnew/3.0.html?highlight=cstringio

    So now would be something like:

    import io
    from matplotlib.figure import Figure     
    from matplotlib import pyplot as plt                 
    
    fig = Figure(figsize=[4,4])                               
    ax = fig.add_axes([.1,.1,.8,.8])                          
    ax.scatter([1,2], [3,4])                                  
    
    buf = io.BytesIO()
    fig.savefig(buf, format='png')
    plt.close(fig)
    data=buf.getvalue()
    
    # In my case I would have used Django for the webpage
    response = HttpResponse(data, content_type='image/png')
    return response
    

提交回复
热议问题