Charting and Graphing Logfiles for Linux Server Admins - page 2
Pie Charts: Who's sending spam?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 (Figure 1).
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 Figure 1: 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 the piechart looks like Figure 2. 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.
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 (Figure 3):
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.
Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.
- 1Linux Top 3: Ubuntu 14.04, Debian Gives Squeeze More Life and Red Hat Goes Atomic
- 2Linux Top 3: CoreOS, Oracle Enterprise Linux 7 and Ubuntu 14.10
- 3Linux Top 3: Debian Dumps SPARC, Ubuntu Takes Over Linux 3.13 and the Core Infrastructure Initiative
- 4Linux Top 3: Fedora, Ubuntu and Gluster Lose Community Leaders
- 5Red Hat Enterprise Linux 7 Finally Hits the Big Time