Matplotlib - an odd problem

koranthala koranthala at gmail.com
Wed Jun 24 23:25:26 EDT 2009


On Jun 25, 2:55 am, Jean-Paul Calderone <exar... at divmod.com> wrote:
> On Wed, 24 Jun 2009 11:38:02 -0700 (PDT), koranthala <koranth... at gmail.com> wrote:
> >Hi,
> >I am using Matplotlib with Django to display charts on the web page.
> >I am facing an odd problem in that, everytime I do a refresh on the
> >web page, the image darkens - and the text becomes little unreadable.
> >5/6 refreshes later, the text becomes completely unreadable.
> >Since I am using Django test server, the same process is being used.
> >i.e. every refresh does not create a new process. When I tried killing
> >the process, the image again cleared.
> >So I think it is sort of memory leak or a saved value which is messing
> >up something.
> >But I cannot seem to find the issue at all.
>
> >The code is as follows -
>
> >import pylab
> >from cStringIO import StringIO
>
> >def print_pie_chart(request):
> >    labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
> >    data = [15,30,45, 10]
> >    pylab.figure(1, figsize=(2,2))
> >    ax = pylab.axes([0.1, 0.1, 0.8, 0.8])
> >    pylab.pie(data, explode=None, labels=labels, autopct='%1.1f%%',
> >shadow=True)
> >    out = StringIO()
> >    pylab.savefig(out, format="PNG")
> >    out.seek(0)
> >    response = HttpResponse()
> >    response['Content-Type'] = 'image/png'
> >    response.write(out.read())
> >    return response
>
> >Can anyone help me out here?
>
> Your code redraws over the same graph over and over again.  You need to
> create a new graph each time you want to draw something new.  It took me
> ages (and help) to figure out how the non-global APIs in matplotlib.
>
> Here's an example:
>
>   from matplotlib.figure import Figure
>   from matplotlib.axes import Axes
>   from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
>
>   fig = Figure(figsize=(9, 9), dpi=100)
>   axe = Axes(fig, (0, 0, 1.0, 1.0))
>   axe.pie(range(5))
>   fig.add_axes(axe)
>
>   canvas = FigureCanvas(fig)
>   canvas.set_size_request(640, 480)
>
>   fig.savefig("foo.png")
>
> Hope this helps,
> Jean-Paul

Thank you Jean-Paul



More information about the Python-list mailing list