Skip to content

D1. A field as an image ​

Goal: a scalar field of a 2D simulation, one value per grid point, shown as colors with the scale beside it.

gnuplot
# a field on a grid (field.dat: x y phi, a block per row) as an image, its values through a palette
set terminal svg size 600,420
set output 'pd01.svg'
set title 'A damped wave'
set xlabel 'x'; set ylabel 'y'; set cblabel '{/Symbol f}'
set size ratio -1
set palette viridis
plot 'field.dat' using 1:2:3 with image notitle
pd01
open SVG ↗

field.dat holds x y phi on a 41 x 31 grid, a block of lines per row of the grid, as most solvers dump a field.

Why it works ​

  • with image draws one rectangle per data point, centred on it, sized by its neighbours: a regular grid becomes its cells, with no interpolation that would invent values between the points.
  • The third column goes through the palette; the color box beside the plot is its scale, labelled by set cblabel.
  • set palette viridis is one of 46 named palettes: perceptually uniform, readable in gray and by color-blind readers. A field with a sign wants a diverging one (set palette vik), centred with set cbrange [-1:1].
  • set size ratio -1 makes a unit of x as long as a unit of y: the field keeps its shape.

Try this ​

  • set palette vik; set cbrange [-1:1]: zero in white, the signs in two colors.
  • set logscale cb for a field over decades (a density, a residual).
  • From Fortran: call fig%image(phi, x, y), phi(column, row) with the rows upward (Fortran Library).

Pitfalls ​

  • with image needs a regular grid. Scattered points want set dgrid3d first (D3), or points colored by value: plot 'f.dat' using 1:2:3 with points pt 7 lc palette.
  • A palette maps the range of the data: two figures compared side by side need the same set cbrange.

Reference: images and palettes. Next: D2. Contours.

foresight is available under your choice of license: GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT (License).