A gnuplot heatmap is a flat 3D plot: read a matrix of values with splot ... matrix, switch on set pm3d map to view it from straight above, and let a palette map values to colors. The script below renders a small temperature grid.
The file matrix.dat is a plain grid of numbers — each row is one row of the map, each column one cell:
12 14 15 13
16 21 24 18
15 23 27 20
11 15 17 14
set pm3d map
set palette defined (0 "#313695", 1 "#ffffbf", 2 "#a50026")
set cbrange [10:30]
set title "Temperature Map"
set xlabel "Column"
set ylabel "Row"
set colorbox
splot "matrix.dat" matrix with pm3d notitle
What each part does
set pm3d map is the heatmap switch: pm3d is gnuplot’s palette-mapped 3D mode, and map projects it onto the xy plane viewed from above, so the plot looks like a 2D grid while still using splot. The matrix keyword in the plot command tells gnuplot the file is a grid of z values — the x and y coordinates are taken from the column and row indices automatically.
Color is controlled separately from geometry. set palette defined (...) builds a gradient from blue through pale yellow to dark red, anchored at relative positions 0, 1 and 2. set cbrange [10:30] fixes which values map to the ends of that gradient — fixing it matters when several maps must share one color meaning. set colorbox ensures the legend bar (the “cb” axis) is shown; label it with set cblabel "Temperature (C)" when the units matter.
Variants and going further
For non-uniform or scattered data, feed xyz triples instead of a matrix: splot "points.dat" using 1:2:3 with pm3d after set pm3d interpolate or a dgrid3d pass to resample onto a grid. A simpler flat alternative for matrix data is the 2D style plot "matrix.dat" matrix with image, which draws each cell as a filled rectangle using the same palette — it cannot interpolate, but it is exact per cell and pairs well with set autoscale fix to avoid half-cell margins.
Common mistakes
- Map appears as a raised 3D surface:
set pm3d mapwas missing, so splot drew its default angled view. The wordmapis what flattens it — add it before the splot command. - Colors look washed out or saturated: the auto-scaled color range follows the data extremes, so one outlier cell flattens everything else. Fix
set cbrangeto the meaningful value window, as in this script.
Related tutorials: 3D Plots with splot, Histograms in Gnuplot. See also the full Gnuplot Tutorials index.
Try it online: Small matrices paste straight into the online terminal as a datablock, making palette experiments quick. Paste the script into the Gnuplot Online terminal and run it in your browser — no install needed. To run gnuplot locally instead, see the installation guide and the download page.
Go deeper: For a systematic walk through gnuplot from first plots to publication-ready figures, see Gnuplot in Action by Philipp K. Janert — Janert is a gnuplot developer, and the book is the most complete tutorial reference for the program. Affiliate link: see our Affiliate Disclosure.
