Creating Pie Chart from Python

Fredrik Lundh fredrik at pythonware.com
Thu Sep 15 12:19:56 EDT 2005


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> 






More information about the Python-list mailing list