Back to blog
Aug 28, 2024
2 min read

Sparklines

Simple visualisations

Sparklines ! From Mr Tufte, a simple but beautiful visualisation : a single line, nothing else, showing a time series. Here’s a simple example:

Sparkline

This is a super-nice piece of Python code that does them as text, for a terminal:

bar = '▁▂▃▄▅▆▇█'
barcount = len(bar)

def sparkline(numbers):
    mn, mx = min(numbers), max(numbers)
    extent = mx - mn
    sparkline = ''.join(bar[min([barcount - 1,
                                 int((n - mn) / extent * barcount)])]
                        for n in numbers)
    return mn, mx, sparkline

And it does this sort of thing …

53
▁▄▃▅▃█▆▁▃▅▅▁▇▇▆▂▂▃▃▄▃▃▅▇▁▁▅▂█▁▄▂█▂▂▃▂▅▃▂▅▁▁▁▂▁▃▁▃▇▁▄▁
54
▂▁▁▁▁▂▂▁▁▃▁▂▅▇▁▄▃▂▁▂▃▂▅▁▁▃▁█▁▁▄▂▇█▄▆▃▄▁▂▂▁▂█▁▄▂▅▂▂▃▁▂▁
55
▇▅▁▄▄▂▃▂▄▂▄▅▂▁▃▁▇▁▄▂▅▃▂▂▁█▅▁▃▂▄▃▄▃▃▂▂▅▅▁▂▇▁▄▃▅▆▄▁▂▂▁▃▄▂

Thanks, Jon Udell