Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating Pie Chart from Python

628 views
Skip to first unread message

Thierry Lam

unread,
Sep 15, 2005, 11:10:10 AM9/15/05
to
Let's say I have the following data:

500 objects:
-100 are red
-300 are blue
-the rest are green

Is there some python package which can represen the above information
in a pie chart?

Thanks
Thierry

Thierry Lam

unread,
Sep 15, 2005, 11:10:17 AM9/15/05
to
Let's say I have the following data:

500 objects:
-100 are red
-300 are blue
-the rest are green

Is there some python package which can represent the above information

Will McGugan

unread,
Sep 15, 2005, 11:19:17 AM9/15/05
to

I wrote a wxPython control to render pretty 3D pie charts (see attached
piechartwindow.py). It can also be used to pre-generate images such as
this..

http://www.foodfileonline.com/static/piecharts/pie01009.jpg

Code is public domain.


Will McGugan
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")

piechartwindow.py

jep...@unpythonic.net

unread,
Sep 15, 2005, 11:26:56 AM9/15/05
to Thierry Lam, pytho...@python.org
There are many.

One choice would be Tkinter's Canvas.

def frac(n): return 360. * n / 500

import Tkinter
c = Tkinter.Canvas(width=100, height=100); c.pack()
c.create_arc((2,2,98,98), fill="red", start=frac(0), extent = frac(100))
c.create_arc((2,2,98,98), fill="blue", start=frac(100), extent = frac(400))
c.create_arc((2,2,98,98), fill="green", start=frac(400), extent = frac(100))
c.mainloop()

Jeff

Fredrik Lundh

unread,
Sep 15, 2005, 11:31:03 AM9/15/05
to pytho...@python.org
Thierry Lam wrote:

on a screen? in a web browser? on a printer?

</F>

Thierry Lam

unread,
Sep 15, 2005, 11:58:18 AM9/15/05
to
In a web browser, having a pie chart in some image format will be great.

Larry Bates

unread,
Sep 15, 2005, 12:08:34 PM9/15/05
to
ReportLab Graphics.

-Larry Bates

Fredrik Lundh

unread,
Sep 15, 2005, 12:19:56 PM9/15/05
to pytho...@python.org
Thierry Lam wrote:

> In a web browser, having a pie chart in some image format will be great.

here's a variation of jepler's tkinter example, using aggdraw to do the
drawing and PIL to generate the image. tweak as necessary.

# http://effbot.org/zone/draw-agg.htm
from aggdraw import *

# http://www.pythonware.com/products/pil/index.htm
import Image

def drawchart(size, data, range):

draw = Draw("RGB", size, "white")

bbox = 10, 10, size[0]-10, size[1]-10

for lo, hi, ink in data:
lo = 360.0 * lo / range
hi = 360.0 * hi / range
draw.pieslice(bbox, lo, hi, Brush(ink))

return Image.fromstring(draw.mode, draw.size, draw.tostring())


data = (
(0, 100, "red"),
(100, 400, "blue"),
(400, 500, "green")
)

im = drawchart((250, 250), data, 500)

im.save("chart.png")

</F>

Ken Seehart

unread,
Sep 15, 2005, 3:51:08 PM9/15/05
to Thierry Lam, pytho...@python.org

What is the user interface context?

Is it a web page? Do you want to create image files with pie chart? If
yes to either of these, try gdchart.

http://www.icewalkers.com/Linux/Software/52020/GDChart.html
http://athani.pair.com/msteed/software/gdchart/download.html


Giovanni Dall'Olio

unread,
Sep 15, 2005, 8:01:24 PM9/15/05
to
PyChart?

paron

unread,
Sep 16, 2005, 6:46:27 AM9/16/05
to
If you can wait a week or two, you can use svg and it will work for IE
or Firefox.

John Hunter

unread,
Sep 16, 2005, 9:18:08 AM9/16/05
to
>>>>> "Thierry" == Thierry Lam <lamth...@gmail.com> writes:

Thierry> Let's say I have the following data: 500 objects: -100
Thierry> are red -300 are blue -the rest are green

Thierry> Is there some python package which can represen the above
Thierry> information in a pie chart?


It looks like in python there is more than one way to make a pie
chart. Here's another

from pylab import figure, pie, show
N, red, blue = 500, 100, 300
green = N - (red + blue)
figure(figsize=(6,6))
pie( (red, blue, green),
labels=('red', 'blue', 'green'),
colors=('red', 'blue', 'green'),)
show()

A screenshot of a slightly more elaborate example is at

http://matplotlib.sourceforge.net/screenshots.html#pie_demo

JDH

Markus Weihs

unread,
Sep 16, 2005, 10:32:25 AM9/16/05
to
Hi!

I tried your script and got the following error:

Traceback (most recent call last):
File "pie_chart.py", line 302, in OnPaint
self.OnDraw()
File "pie_chart.py", line 336, in OnDraw
segment.Draw(self.angle, self.rot, self.explode)
File "pie_chart.py", line 46, in Draw
glPopMatrix()
OpenGL.GL.GLerror: [Errno 1281] invalid value

What am I doing wrong?


Regards, Markus


Thierry Lam

unread,
Sep 16, 2005, 10:34:01 AM9/16/05
to
Those python pie chart add ons are not very useful. For my specific pie
chart, I have some 6-8 items to show up and some of them occupy only
2-5% of the pie. This cause the names and percentages to overlap each
other.

Will McGugan

unread,
Sep 16, 2005, 10:53:39 AM9/16/05
to

I'm not sure. Thats a strange place for it to fail. Do you have the
latest version of pyOpenGL / wxPython?

Its only been tested on Windows, but it just uses basic OpenGL so there
is not that much to go wrong..

0 new messages