Plot matplotlib on the Web

前端 未结 2 1036
粉色の甜心
粉色の甜心 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:55

    First you need a page to load a url from the webserver controller which generates the image:

    
    

    Then, embed the matplotlib code into the makegraph controller. You just need to capture the canvas rendered PNG in a memory buffer, then create an HTTP response and write the bytes back to the browser:

    import cStringIO
    from matplotlib.figure import Figure                      
    from matplotlib.backends.backend_agg import FigureCanvasAgg
    
    fig = Figure(figsize=[4,4])                               
    ax = fig.add_axes([.1,.1,.8,.8])                          
    ax.scatter([1,2], [3,4])                                  
    canvas = FigureCanvasAgg(fig)
    
    # write image data to a string buffer and get the PNG image bytes
    buf = cStringIO.StringIO()
    canvas.print_png(buf)
    data = buf.getvalue()
    
    # pseudo-code for generating the http response from your
    # webserver, and writing the bytes back to the browser.
    # replace this with corresponding code for your web framework
    headers = {
        'Content-Type': 'image/png',
        'Content-Length': len(data)
        }
    response.write(200, 'OK', headers, data)
    

    Note: you may want to add caching for these if they're frequently generated with the same arguments, e.g. construct a key from the args and write the image data to memcache, then check memcache before regenerating the graph.

提交回复
热议问题