buli » blog

  • About
  • Portfolio

 

03
Jan

MATLAB: how to draw a great heatmap?

By buli|MATLAB|6 Comments

Drawing heat maps, which represent matrix data using colors, in MATLAB is fairy simple, but there is a bit of confusion about it. It’s mainly because it’s easier to find quiet unintuitive Bioinformatics Toolbox’s HeatMap object than the standard way of doing it which is imagesc(matrix) supported with colormap(scheme).

Rainbow generated using imagesc() function

If you only need code snippet, here it is:

Heatmap in MATLAB: code snippet
1
2
3
4
A = 1:50;          % matrix to draw
colormap('hot');   % set colormap
imagesc(A);        % draw image and scale colormap to values range
colorbar;          % show color scale

For the list of all colormaps available by default, see MathWorks’ documentation on colormap. In this article below, I’ll try to explain which one I would choose in most cases and why.

Why default colormap is not good?

Some time ago I was working with thermal images of a palm. Data was actually the temperature, so heatmaps seem to be the best way to represent it ;) Using image and imagesc I made pretty images that appeared good on my screen, but then following issues arose:

  • How would it look printed in black & white and on my Kindle?
  • How would it look to those 7-8% males color vision deficiency?
  • How will it look on this 10-years-old projector where I need to present my results?
  • How will it look to my dog (kidding, but it’s an interesting question too)

Well, first three are quite easy to check. Few standard colormaps converted to grayscale are displayed in figure 1, below. For simulation of how users with various color vision deficiencies, see this interesting diagram by Vlad Atanasiu (I couldn’t confirm it, but I believe it has some solid foundations). Projector didn’t make much problems in my case, but it’s woth to check if it has enough contrast to see all important color differences.

Colormaps: original and converted to grayscale
Colormaps: original and converted to grayscale. From the top: ‘default’, ‘hot’, and ‘gray’

As we can see here, the ‘default’ (or ‘jet’) colormap, in grayscale turns differently than we might expect. The highest values are not the brightest, so the information on the picture will just look wrong!

To scale or not to scale?

It might be a cliché, but this might be the good place to bring up the difference between image and imagesc.

image(X)
if drawing 2D array (MxN), by default the data will be directly mapped to colormap colors (with values 1…64), or the closest color available in the colormap
imagesc(X)
if drawing 2D array (MxN), data will be first scaled to the full colormap range and just then drawn

So the codes image(1:256) and imagesc(1:256) will render different results, as shown in figure 2.

MATLAB: image vs imagesc
Vector [1:256] drawn using image (top) and imagesc (bottom) functions in MATLAB

Be aware that when you scale the colormap to image, images will not be easily comparable for people, and sometimes might be misleading.

Which color is warmer?

Going deeper into «which colormap is best» we need to make the decision whether some color A is warmer than color B. And it is actually not so simple. I was hoping that reading about color theory would give me a clue, but now I think such strict definition may not be possible.

Generally, in color theory warm colors are those associated with daylight and sunset or fire, while cool colors have their peak somewhere blue. This is the way the term warm color and cool color are used in paintings etc. Also, not only hue makes color cold or warm, but also its saturation or intensity. And contrast, as people really perceive color differences than colors itself.

We also have a physical measure color temperature. The problem is that the higher temperatures are blue there, while lower go towards red hues, so it’s just not how we intuitively perceive what is warmer.

The colors are not a simple thing and I’m not an expert, so I’ll just end this section with conclusion that the question «Which color is warmer?» is not an easy one.

Lessons learned

Below there are two heatmaps i generated for my thermal hand recognition project. The default colormap (on the right) seems to look good on the screen, but the one on the left actually represents the data better.

Thermal hand images using two colormaps: hot (left image) and jet (default, right image)
Thermal hand images using two colormaps: hot (left image) and jet (default, right image)

So now here is the conclusion. If you draw a heatmap with a plain range of values, think whether the 'gray' colorscale woudn’t be enough. It will work fine no matter if it’s on the screen, paper, or e-ink display, and should be read without problems by people with some form of color impairment. If you want color, go for 'hot' colormap rather than using default ‘jet’. ‘Hot’ isn’t actually worse than grayscale. Be careful when scaling colormaps. And, what is most important, make sure the figure presents the data in a way that is easy to understand and is well described ;)

(You would probably also not want this comic to be about you)

Share this:

Add your comment →

6 Comments

  1. Thanks for this post. After wasting a couple of hours customizing the heatmap function, I landed on this post of yours. This way of doing heatmaps is a lot more flexible and easier. Thanks once again!

    By: Dhrumin S Reply →
    5 May 2013 at 18:37
    • I’m glad that code snippet helped :-) HeatMap is probably a powerful object to work with, but also not what I was expecting to find when drawing my heatmap.

      By: buli Author
      5 May 2013 at 18:48
  2. hey, do you know, how to smooth heat map, I have only 32×24 measuring points, so it doesn’t look too attractive.

    By: Michal Reply →
    12 May 2013 at 09:34
    • In Matlab you could use interpolation eg. by resizing your matrix to a larger one before drawing, like:

      scale = 16;
      imgMatrix = imresize(imgMatrix, size(imgMatrix)*scale);

      The effect would be like on http://buli.waw.pl/tmp/imresize.jpg image. The other idea is to use some blur effect outside of MATLAB, resulting with something like http://buli.waw.pl/tmp/heatmap_blur.png

      But in general I would not do it at all, as it looks like creating data you don’t really have, which might be misleading (as you don’t know hou would the data look in higher resolution) ;-)

      By: buli Author
      12 May 2013 at 10:04
  3. I’m working very first time with heat map. So kind of confused what to do. Can you please provide a little more explanation on how did you do the heat map with the hand shape? I have a matrix of points X,Y which is actually the pixels of a given image. I want to generate the heat map on that image with the matrix I have. Is it even doable? I apologize if I ask naive question. Please help

    By: Pow Reply →
    16 May 2013 at 21:02
    • Hey, you need to transform your list of points into a matrix first. The easiest way seems accumarray() function. Here’s how it works: http://buli.waw.pl/tmp/convert_list_of_points_to_matrix.png – I hope this quick draft helps. There are no naive questions, I used to do the same thing in much more complicated way and now I found this useful function thanks to your question ;)
      The hand shape itself is actually a data obtained from a device we use in lab, so It’s not a standard MATLAB resource.

      By: buli Author
      17 May 2013 at 08:37

Leave a Reply Cancel reply

View More Posts:
  • ←
  • →

Recent Posts

  • Compress all files on remote server (using PHP)
  • Introducing mRMR-turbo
  • Add feedly to Chrome RSS Reader
  • Hyper-V vs. high resolutions (1680×1050 etc.)
  • WebMatrix: how to display PHP errors

Categories

  • .NET
  • Bez kategorii
  • MATLAB
  • Office
  • Organizacyjne
  • SQL Server
  • Web development
  • Windows

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Powered by the inLine Minimal WordPress Theme

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items.
      Cancel