Creating Infographics in Python

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

The infographic is a simple 5 x 5 grid with numbers inside from 0 to 1. The grid squares will then be colored 0 = white 1 = blue 0.5 - pale blue.

Matplotlib may have been used, but I could not find or combine examples that would help me understand how to do this.

Any insight, sample code, or library direction will really help.

Matt relationship

+4
source share
3 answers

It depends on what you need to do with the graph, when you have one, Matplotlib allows you to interactively display the graph on the screen, save it as a vector, PDF or bitmap, etc.

If you choose this framework, imshow will do everything 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 more information on colormap check this link , and here for imshow - or just use help(colors.LinearSegmentedColormap) and help(imshow) .

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

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


Edit

however i'm looking to display numbers in a grid

To make 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") 
+4
source

PyCairo is your friend. A 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 can find this tutorial .

+2
source

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

Here is an example of a quick and dirty:

 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

+2
source

All Articles