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 |
![]() |
| Ten times interpolation |
Colours
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 |
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/





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:
ReplyDeleteset 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.
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?
DeleteThanks for this post. I was looking for this information and haven't found it in the offical documentation for gnuplot.
ReplyDeleteThanks 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/
ReplyDeleteIt would also be helpful to show how to add contours or arbitrary lines to such a colormap.
This comment has been removed by the author.
ReplyDelete