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
Comments (3)
0
hmehrpooya 13 years ago
0
mike-ellis 14 years ago
0
vincentdavis 14 years ago