Monday, 12 May 2014

How to Create a Heatmap in Gnuplot

Simple Heatmap


We're all familiar with the usual line graph which plots a dependent variable against an independent one. However, sometimes you have two independent variables in which case it is often convenient to plot a heatmap to show the effects rather than plotting multiple lines on a regular line-graph. Here's how to do it in Gnuplot.

Throuhgout, I assume you have the data stored in columns in a file called "source.dat" where the first two columns contain the independent variables and the third column is the dependent one. I'm going to use a simple example with the formula z = x2 + y2. The input data consists of 11 rows with x and y having values 0 to 10 inclusive. The following gnuplot commands produce a simple heatmap:

set view map
set dgrid3d
splot "source.dat" using 1:2:3 with pm3d

The resulting plot is not particularly pretty:
A simple gnuplot heatmap is not pretty

Improvements

One of the things we can do to make the map look better is to smooth it out. We can do this by either having more data points or by getting gnuplot to artificially introduce more data points. Use the following command:

set pm3d interpolate x,y

where "x" and "y" are the number of additional points in the x and y axes. So if "x" is set to 4 then Gnuplot will interpolate with 4 times as many points in the x-axis.

With 4 times interpolation the above map becomes:
Interpolating the data points produces a more smoothed out graph
With 10 times interpolation we get:
Ten times interpolation

Colours

Gnuplots' default colours are different to those of MATLAB but the following commands can change the colours to match:

set palette defined (0 0 0 0.5, 1 0 0 1, 2 0 0.5 1, 3 0 1 1, 4 0.5 1 0.5, 5 1 1 0, 6 1 0.5 0, 7 1 0 0, 8 0.5 0 0)

The resultant map:
Gnuplot heatmap with MATLAB colours
Of course, it is important to remember your audience. When producing heatmaps for reports you should bear in mind that a large proportion (if not the vast majority) of those who read the reports will read it in black and white and so your heatmap ought to be readable in monochrome. Often if you print a colour graph in black and white you just cannot interpret it and this is especially true on a heatmap where the only thing you have to play with is the colouring. Therefore, it may be advisable to produce the map in black and white to begin with by using the command:

set pallete grey

This results in:
A heatmap produced in monochrome 

Final Note on Labels

Obviously you want to produce graphs with labels so I'll just leave here the commands for setting the fonts of the different labels and numbers around the graph:

set xlabel font "Times-Roman,16"
set ylabel font "Times-Roman,16"
set xtics font "Times-Roman,14"
set ytics font "Times-Roman,14"
set title font "Times-Roman,20"

References:

http://www.gnuplotting.org/matlab-colorbar-with-gnuplot/
http://www.gnuplotting.org/interpolation-of-heat-maps/comment-page-1/

5 comments:

  1. Great post! Thanks! :) I've noticed that plotting in this way does not capture the effect of grid refinement on the resulting plot. E.g. if I do:

    set view map
    splot "file.dat"

    Then I can plot ONLY the data points. HOW CAN I THEN create a heat color map, so that I can colour those points? In this way, IF I INCREASE grid refinement, more points will be plotted under the color map, thus capturing the effect of refining my grid.

    ReplyDelete
    Replies
    1. Hi Eduardo, I'm not getting your question. Do you want to have a color map with the desired points marked with other colors in the map?

      Delete
  2. Thanks for this post. I was looking for this information and haven't found it in the offical documentation for gnuplot.

    ReplyDelete
  3. Thanks for the description. A couple suggestions: It's better to use a different color palette instead of the rainbow colormap, and here is the reason why: http://www.kennethmoreland.com/color-maps/
    It would also be helpful to show how to add contours or arbitrary lines to such a colormap.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete