Thursday, December 17, 2009

Matplotlib in OS X: Heat Maps


I've been exploring matplotlib. It's complex enough that I probably need to get a book. I decided to revisit the problem of producing heat maps. In the last couple of posts (here and here), I mentioned a couple of methods that might be used: (scatter, pcolormesh, and pcolor).

But, today I'm going to use matplotlib.patches.Rectangle. Rather than post the code (140 lines in two scripts), I made a zipped Archive that includes:

• a directory containing three data files
(counts, topo.colors, colors for the rownames)
• Helper.py and HeatMapper.py
• example.pdf

Above is a part of the figure the code draws The whole thing is up on Dropbox here.

The code to draw the boxes is:

    p = Rectangle((x,y),width=dx,height=dy,
facecolor=box_color,edgecolor='w')
ax.add_patch(p)
if n == 0: continue
t = ax.text(x+dx/2,y+dy/2,str(n),
color=label_color,fontsize=12,
fontname='Helvetica',
ha='center',va='center')


It took significantly less time to write this than the Cocoa app that does the same thing, and I think it gains a lot in flexibility.

Here is an example in black and white. It uses the result from binCounts() as the color (i.e. gray).


No comments: