May 19 2007
Sparklines in python
Edward Tufte, the outspoken guru of data visualization, has long been an advocate of clear and concise (almost minimalist) graphical representations of data. He’s got a lot of great ideas relevant to cartography (my cartography course at Humboldt State used his book “The Visual Display of Quantitative Information” as our text).
One of the coolest ideas are “sparklines” which he describes as “data-intense, design-simple, word-sized graphics”. Instead of standalone charts that are often placed on their own and separate from the text that discusses them, sparklines are meant to be placed in-line with the text and provide memorable, simple and contextually-relevant data to support the surrounding text. For example:
Now of course I had to figure out how to produce these in python. Theres a great cgi application, written in python by Joe Gregorio, that does sparklines. I needed something that was abstracted away from the CGI framework, more of a proper python module. Replacing all the CGI-specific code was straightforward and I came up with a standalone sparkline python module ( View / Download the Source Code. ) The only dependencies are python and the python imaging library.
In the minimalist spirt of sparklines, the interface was kept simple. First you create a list of data values then simply pass the list to one of the sparkline generators:
import spark
a = [32.5,35.2,39.9,40.8,43.9,48.2,50.5,51.9,53.1,55.9,60.7,64.4]
spark.sparkline_smooth(a).show()
Or if you prefer a more discrete, bar-graph-style instead of a smooth line:
spark.sparkline_discrete(a).show()
There’s plenty of room for configuration. For example, in the national debt example above I wanted to keep the y axis at the same scale (instead of the default min-max scaling) and make each step 6 pixels wide:
spark.sparkline_smooth(a, dmin=30,dmax=70, step=6).show()
How does this relate to cartography? GIS typically takes a snapshot representation of earth, frozen in time. Since sparklines seem particularly good at representing change-over-time, it could be an interesting way to add a time dimension to a 2-D map. For example, instead of just displaying country polygons with labels, you could place a sparkline right under the label showing the population changes over the last century. It seems like it would be an ideal way to embed alot of useful information into a small map.
Anyone know of any good examples?
Hey Matt, nice demo of sparklines! I came across a thread on the Tufte site a while back and fused around with sparklines in PHP, in particular via the Drupal Sparkline module. I posted some notes on my experiment here. It also turns out that there is a nice LATEX package for adding sparklines to any pdfLATEX project.
cheers,
dylan
Very cool Matt!
Tufte is awesome. I have 3 of his books sitting on my desk. There are some sparkline libs for ruby too;
http://nubyonrails.topfunky.com/articles/2005/07/28/sparklines-graph-library-for-ruby
I’m always wanting to use them but for ecological data there never seems to be enough points to make a smooth sparkline! It’s really best for high resolution visualizations like oceanographic data or stock prices.
I have used sparklines fairly intensively at the weather startup a partner and I have founded: http://www.stormpulse.com. We’re using them to illustrate wind speed and pressure change over time.
For an example of lots of sparklines, see the 2005 atlantic hurricane season here: http://www.stormpulse.com/2005 (or any other busy year (e.g. 2004), for that matter).
Oh, and in case you were wondering, our site is 100% Python (pylons) framework.
The mapping window doesn’t use an API–it’s homegrown with ActionScript 2.0–for better or for worse.
[…] to generate these things, and I tried to find the simplest one implemented in Python. I found Matthew Perry’s module, installed PIL, and I was good to […]