How to plot heatmap with matplotlib?

Dear all,

Heatmap (like those on the page http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/r/heatmap/) is a frequently used type of image in microarray data analysis. However, it seems there are no convenient functions in matplotlib to plot heatmap (please correct me if I was wrong), so I'm planning to write my own.

Let me take the heatmap by the link http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/r/heatmap/scaled_color_key.png as an example, which is produced by R.

With my limited knowledge and expertise of matplotlib, I have the following questions and I hope you guys could help me.

1) I tend to use pcolor to draw the colormap in the central area. However, I've seen a lot of examples draw colormap with imshow.

     What's the difference between pcolor and imshow?
     Shall I use pcolor or imshow to produce the heatmap in the link above?

2) How to draw the dendrograms on the top and left of the colormap?

     I got hints from http://matplotlib.sourceforge.net/examples/axes_grid/scatter_hist.html on how to append axes to current plot, but I still have now idea how to draw the dengrograms.

3) How to draw the column side colormap (the smaller one) between the top dendrogram and the big colormap?

4) I can use colorbar to draw a colorbar, but how to place the colorbar on the topleft of the image just as the R heatmap does?

5) Any other suggestions on how to draw the heatmap?

Thanks and any help will be greatly appreciated.

Regards,
Jianfeng

Dear all,

Heatmap (like those on the page
http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/r/heatmap/)
is a frequently used type of image in microarray data analysis. However,
it seems there are no convenient functions in matplotlib to plot heatmap
(please correct me if I was wrong), so I'm planning to write my own.

Let me take the heatmap by the link
http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/r/heatmap/scaled_color_key.png
as an example, which is produced by R.

With my limited knowledge and expertise of matplotlib, I have the
following questions and I hope you guys could help me.

1) I tend to use pcolor to draw the colormap in the central area.
However, I've seen a lot of examples draw colormap with imshow.

What's the difference between pcolor and imshow?
Shall I use pcolor or imshow to produce the heatmap in the link above?

One difference between pcolor and imshow is that pcolor lines up the
bottom-left corner of each square with the co-ordinates given, whereas
imshow puts the grid center at those co-ordinates. My guess would be
(not having implemented a heatmap) that pcolor would be the more
useful to you to try first for this reason.

2) How to draw the dendrograms on the top and left of the colormap?

I got hints from

http://matplotlib.sourceforge.net/examples/axes_grid/scatter_hist.html
on how to append axes to current plot, but I still have now idea how to
draw the dengrograms.

A quick google search suggests that scipy-cluster might help you out
with the dendrogram drawing. I think I would try using separate axes
objects for the heatmap and the dendrograms, and switch off the frames
of the dendrogram axes.

3) How to draw the column side colormap (the smaller one) between the
top dendrogram and the big colormap?

This could another axes object too. You can use the sharex keyword to
add_axes to get it to have the same x scale as the big axes.

4) I can use colorbar to draw a colorbar, but how to place the colorbar
on the topleft of the image just as the R heatmap does?

Again, the answer is to manually create the axes for your colorbar,
and then specify those axes in the call to colorbar using the `cax`
keyword.

5) Any other suggestions on how to draw the heatmap?

I suggest that you give it a try and post your efforts here if there
are any problems. We'll also be keen to see the final result, if you'd
like to share it.

I hope that helps at least a little. Happy coding,

Angus

···

On 25 September 2011 06:59, fdu.xiaojf@...287... <fdu.xiaojf@...287...> wrote:
--
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh

`Hi all,

    Thanks to Angus, and thanks to those guys who contributed to the

internet. I wrote a prototype of a heatmap function with their
help.

  ``

    Could some one help to review my code? Any comments will be

greatly appreciated.`` I would like to
continue to refine the code, and hope it can be of any help to
the community someday. `

`

  ``I still have some questions:

        1) The number of rows and columns could vary significantly

and the heatmap may look ugly if the parameters are not set
right. `` How to set the appropriate positions
of all axes and the spaces between those axes? And how to set up
appropriate font size?

        2) If there are too many rows or columns, it may not be

efficient to draw the labels of each row or column one bye one
with the text() function. Is there a better way to draw many
text at once at different locations?

    Thanks a lot.``

    Regards,

    Jianfeng

  `

heatmap.py (4.87 KB)

···

fdu.xiaojf@…287…<fdu.xiaojf@…287…>http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/r/heatmap/http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/r/heatmap/scaled_color_key.pnghttp://matplotlib.sourceforge.net/examples/axes_grid/scatter_hist.html