By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Trying to render a pie chart

P: n/a
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

May 30 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.