If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

matplolib howto

Store the following code in a model file or in a module and import it from a controller that needs it

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

def plot(title='title',xlab='x',ylab='y',mode='plot',
         data={'xxx':[(0,0),(1,1),(1,2),(3,3)],
               'yyy':[(0,0,.2,.2),(2,1,0.2,0.2),(2,2,0.2,0.2),(3,3,0.2,0.3)]}):
    fig=Figure()
    fig.set_facecolor('white')
    ax=fig.add_subplot(111)
    if title: ax.set_title(title)
    if xlab: ax.set_xlabel(xlab)
    if ylab: ax.set_ylabel(ylab)
    legend=[]
    keys=sorted(data)
    for key in keys:
        stream = data[key]
        (x,y)=([],[])
        for point in stream:
            x.append(point[0])
            y.append(point[1])
        if mode=='plot':
            ell=ax.plot(x, y)
            legend.append((ell,key))
        if mode=='hist':
            ell=ax.hist(y,20)            
    if legend:
        ax.legend([x for (x,y) in legend], [y for (x,y) in legend], 
                  'upper right', shadow=True)
    canvas=FigureCanvas(fig)
    stream=cStringIO.StringIO()
    canvas.print_png(stream)
    return stream.getvalue()

def pcolor2d(title='title',xlab='x',ylab='y',
             z=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]]):
    fig=Figure()
    fig.set_facecolor('white')
    ax=fig.add_subplot(111)
    if title: ax.set_title(title)
    if xlab: ax.set_xlabel(xlab)
    if ylab: ax.set_ylabel(ylab)
    image=ax.imshow(z)
    image.set_interpolation('bilinear')
    canvas=FigureCanvas(fig)
    stream=cStringIO.StringIO()
    canvas.print_png(stream)
    return stream.getvalue()

then try actions like the following:

def myplot():
    response.headers['Content-Type']='image/png'
    return plot(data={'my plot':[(0,0),(1,1),(2,4),(3,9),(4,16)]})

def myhist():
    response.headers['Content-Type']='image/png'
    return plot(data={'my plot':[(0,0),(1,1),(2,4),(3,9),(4,16)]},mode='hist')

def myplot2():
    response.headers['Content-Type']='image/png'
    return pcolor2dt(z=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]])

and call them with

http://host:port/app/controller/myplot.png
http://host:port/app/controller/myhist.png
http://host:port/app/controller/myplot2.png

Related slices

Comments (3)

  • Login to post



  • 0
    vincentdavis 14 years ago
    Thanks for this example. Is there information about how to install web2py onto an existing python/matplotlib installation. I was having problems getting it to install on Enthought python.

  • 0
    mike-ellis 14 years ago
    Thanks, this is a useful start. As written, it puts the each plot by itself on a page in the browser. I think the majority of applications would want to show the plots embedded in a page with other content. It would be nice if the example were extended to show the best way to do this. BTW, there's a typo in myplot2(). Should change "pcolor2dt" to "pcolor2d" to match the function name defined in the module.

  • 0
    hmehrpooya 13 years ago
    Hi Guys, Does anyone know how to install matplotlib on web2py. my OS is Win 7-64 and Python 2.6 In the Dna talk and in the book it looks so straight forward i don't know why i got stock. Please help me : ) I need to install LXML, matplotlib Thanks, Hadi

Hosting graciously provided by:
Python Anywhere