Question

I want to create a simple infographic in python. Matplotlib seems to have a lot of features but nothing that covers off my simple heatmap grid example.

The infographic is a simple 5 x 5 grid with numbers inside ranging from 0 to 1. The grid squares would then be coloured in 0=white 1=blue 0.5 being a pale blue.

Matplotlib could probably be used but I couldn't find or combine any examples that offered insight into generating this.

Any insight, example code or library direction would really help

Regards Matt

Was it helpful?

Solution

It depends what you need to do with the graph once you have it, Matplotlib allows you to interactively show the graph on the screen, save it in either vector, pdf or bitmap format, and more.

If you opt for this framework, imshow will do what you need, here is an example:

# Just some data to test:
from random import gauss
a = [[gauss(0, 10) for i in xrange(0, 5)] for j in xrange(0,5)]

from pylab import * # or just launch "IPython -pylab" from the command line

# We create a custom colormap:
myblue = cm.colors.LinearSegmentedColormap("myblue", {
    'red':   [(0, 1, 1), (1, 0, 0)], 
    'green': [(0, 1, 1), (1, 0, 0)],
    'blue':  [(0, 1, 1), (1, 1, 1)]})

# Plotting the graph:
imshow(a, cmap=myblue)

For further details on the colormap check this link, and here is the link for imshow - or simply use help(colors.LinearSegmentedColormap) and help(imshow).

alt text http://img522.imageshack.us/img522/6230/bluep.png

(note that this is the result with the standard options, you can add a grid, change the filtering and so on).


Edit

however I'm looking to display the numbers in the grid

To keep it simple:

for i in xrange(0,5):
    for j in xrange(0,5):
        text(i, j,
             "{0:5.2f}".format(a[i][j]),
             horizontalalignment="center",
             verticalalignment="center")

OTHER TIPS

PyCairo is your friend. Simple example:

from __future__ import with_statement
import cairo
img = cairo.ImageSurface(cairo.FORMAT_ARGB32,100,100)
g = cairo.Context(img)
for x in range(0,100,10):
    for y in range(0,100,10):
        g.set_source_rgb(.1 + x/100.0, 0, .1 + y/100.0)
        g.rectangle(x,y,10,10)
        g.fill()
with open('test.png','wb') as f:
    img.write_to_png(f)

output

You might find this tutorial helpful.

One possibility would be to generate SVG from python. You can view SVG in Firefox or Inkscape.

Here's a quick-and-dirty example:

import random

def square(x, y, value):
    r, g, b = value * 255, value * 255, 255
    s = '<rect x="%d" y="%d" width="1" height="1" style="fill:rgb(%d,%d,%d);"/>' % (x, y, r, g, b)
    t = '<text x="%d" y="%d" font-size=".2" fill="yellow">%f</text>' % (x, y + 1, value)
    return s + '\n' + t

print('''
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="100%" height="100%" version="1.1" viewBox="0 0 5 5"
xmlns="http://www.w3.org/2000/svg">
''')
for x in range(0, 5):
    for y in range(0, 5):
        print(square(x, y, random.random()))

print('</svg>')

alt text http://www.imagechicken.com/uploads/1257184721026098800.png

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top