我想通过编写web2py函数myplot4()在web2py中绘制一个sympy情节图并使用它来调用它 http://host:port/app/controller/myplot4.png
这是一个在python shell中工作的情节的演示同情代码。
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')
p1 = plot(x*x)
p2 = plot(x)
p1.append(p2[0])
p1
我在http://www.web2pyslices.com/slice/show/1357/matplotlib-howto
中找到了食谱此配方工作正常,但它导入了FigureCanvasAgg和Figure,而在同意中导入是绘图函数
有人可以告诉我如何根据这个食谱(或其他方式)编写类似的功能来绘制web2py中的sympy情节
from matplotlib.backends.backend_agg
import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
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()
然后尝试以下操作:
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]])
并使用
调用它们答案 0 :(得分:0)
我通过实验找到答案,这很简单。
def plot_sympy():
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import io
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')
p1 = plot(x*x)
p2 = plot(x)
p1.append(p2[0])
s = io.BytesIO()
p1.save(s)
fig=Figure()
canvas=FigureCanvas(fig)
canvas.print_tif(s)
return s.getvalue()
def myplot4():
response.headers['Content-Type']='image/png'
return plot_sympy()