如何在Plone中使用matplotlib

Nan*_*mar 3 plone matplotlib

我正在使用Plone 4.3和matplotlib.我有绘图窗口,但我想在页面模板中渲染绘图窗口.有些人建议我使用HTML5 canvas在模板页面中渲染绘图窗口.但我无法理解这个概念.所以任何人都可以帮助我?

acl*_*ark 8

创建浏览器视图,设置标题类型,并返回图像数据,例如

from zope.publisher.browser import BrowserPage


class PloneMatPlotLib(BrowserPage):
    """
    """

    def __call__(self):

        import cStringIO
        from matplotlib.figure import Figure
        from matplotlib.backends.backend_agg import FigureCanvasAgg

        x, y = 4, 4

        fig = Figure(figsize=[x, y])
        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()

        # write image bytes back to the browser
        self.request.response.setHeader("Content-type", "image/png")
        return data
Run Code Online (Sandbox Code Playgroud)

然后,您可以调用此视图TTW(通过网络)获取图像,例如http://localhost:8080/Plone/@@plone_matplotlib.或者在页面模板中,例如:

<div metal:use-macro="here/main_template/macros/master">
    <div metal:fill-slot="main">
       <img tal:attributes="src python:context.absolute_url() + '/@@plone_matplotlib'">
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

更多信息: