GuidesPretty Python Plotting With CairoPlot Page 2

Pretty Python Plotting With CairoPlot Page 2

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




Generating a Pie Chart

How do you generate a pie chart from a dictionary? It only takes one line:

CairoPlot.pie_plot("piechart", rejected, 500, 500, None, True, False, None)

CairoPlot will produce a graphics file named pie.svg.

The arguments are:

pie_plot(name,
data,
width, height,
background=None,
gradient=False, shadow=False,
colors=None)

name is the filename. If you include an extension such as .jpg, CairoPlot will use that format instead of SVG format, in case you need a graphic that even IE users can view on a website.

data, of course, is the dictionary of values.

width and height are the desired size of the plot. Notice that CairoPlot leaves quite a bit of extra space around the outside of the pie, so plan accordingly.

background lets you specify a background color as a tuple of red, green and blue, so background=(0, 1, 0) would give a solid green background. You can also pass a Cairo gradient here. gradient specifies whether the pie slices themselves should show a gradient, which makes the plot prettier. shadow lets you add a drop shadow on the whole piechart, and you can pass an array of custom colors–again, tuples or gradients–if you don’t like the default colors. The colors list must have exactly the same number of entries as the data dictionary.

A minor problem with the chart in we just created: It turns out most hosts with invalid HELO addresses aren’t resolvable at all, and the rest of the chart gets all squinched into a tiny piece of pie. What happens if you toss out all those unknowns? You can do that by adding one else clause after the if hostname:

        if hostname :
dot = hostname.rfind(".")
if dot >= 0 :
ext = hostname[dot+1:]
else :
continue

Run that, and you get a competely different pie chat Quite interesting! I had no idea, before writing this example, that I got so much spam from Israel and Brazil compared to other countries. Sometimes a picture really is worth a thousand words.

Bar Charts

CairoPlot makes pretty bar charts, too. Unfortunately, CairoPlot’s various methods aren’t consistent about their input, and bar_plot wants a list, not a dictionary.

No problem! Just convert that dictionary to two lists — one for the labels, one for the data — and call bar_plot:

h_labels = [ k for k in rejected.keys() ]
rejlist = [ rejected[k] for k in rejected.keys() ]
CairoPlot.bar_plot ('bars', rejlist, 500, 400,
border=5, three_dimension=True,
h_labels=h_labels)

Again, you can pass a list of colors if you want custom colors, and there are a few other options available, like background, grid, rounded_corners, h_bounds and v_bounds, and of course v_labels as well as h_labels.

Of course, CairoPlot can do other types of graphs as well. There’s some documentation here, or you can use the interactive Python interpreter and type

import CairoPlot
help(CairoPlot.pie_plot)

Eventually, CairoPlot may move to Sourceforge and have a more organized website. But in the meantime, if you experiment a bit, you’ll find it’s one of the best packages around for making pretty, colorful graphs.

Follow ServerWatch on Twitter

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories