How to use matplotlib in Plone

I am using Plone 4.3 with matplotlib. I have a graph window, but I want to display the graph window in the page template. Some people have suggested that I use HTML5 canvasto display a chart window on a template page. But I can not understand this concept. So can anyone help me?

+3
source share
2 answers

Create a browser view, set the title type and return image data, for example.

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

Then you can call this TTW view (via the Internet) to get an image, for example. http://localhost:8080/Plone/@@plone_matplotlib. Or in a page template, for example:

<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>

More details here:

+8
source

matplotlib plone 5. , . , .

0

All Articles