Trying to render a pie chart

erikcw erikwickstrom at gmail.com
Wed May 30 15:05:38 EDT 2007


HI all,

I'm trying to use Matplotlib to render a pie chart for a django site
I'm working on.

I can't figure out how to get rid of the grey background, or make the
dimensions square (it's getting stretched).

Here is the code:

def oldchart(request):
    from PIL import Image as PILImage
    from matplotlib.backends.backend_agg import FigureCanvasAgg as
FigureCanvas
    from matplotlib.figure import Figure
    from StringIO import StringIO
    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    #ax.plot([1,2,3])
    labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
    fracs = [15,30,45, 10]
    ax.pie(fracs, labels=labels)
    #ax.set_title('hi mom')
    ax.grid(True)
    #ax.set_xlabel('time')
    #ax.set_ylabel('volts')
    canvas.draw()
    size = canvas.get_renderer().get_canvas_width_height()
    buf=canvas.tostring_rgb()
    im=PILImage.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1)
    imdata=StringIO()
    im.save(imdata, format='JPEG')
    response = HttpResponse(imdata.getvalue(), mimetype='image/jpeg')
    return response

Thanks for your help!
Erik




More information about the Python-list mailing list