Gnuplot Filled Curves Example

Filled curves shade the area between two curves (or between a curve and an axis), which makes differences, ranges, and uncertainty bands visible at a glance.

f(x) = sin(x) + 0.6
g(x) = sin(x) - 0.6
set xrange [-pi:pi]
set yrange [-2:2]
set title "Band Between sin(x) + 0.6 and sin(x) - 0.6"
set xlabel "x"
set ylabel "y"
set grid
set style fill solid 0.35 border -1
plot '+' using 1:(f($1)):(g($1)) with filledcurves linecolor rgb "#5e2d79" title "Band between curves", \
     f(x) with lines linewidth 2 linecolor rgb "#333333" title "Upper edge", \
     g(x) with lines linewidth 2 linecolor rgb "#333333" title "Lower edge"

How the script works

The two functions f(x) and g(x) are a sine wave shifted up and down by 0.6, so the band between them has constant width — a clean first case. Defining them by name keeps the plot command short.

The fill itself is the '+' element. The pseudo-file '+' makes gnuplot sample the current x range and hand each sample to the using expression: using 1:(f($1)):(g($1)) produces three columns per sample — x, the upper value, the lower value — and with filledcurves shades everything between the second and third columns. set style fill solid 0.35 makes the fill semi-transparent so the grid still shows through.

The two extra plot elements redraw the band edges as solid lines. A filled region alone has soft edges; drawing the boundary curves explicitly is what makes the figure read precisely.

Run it online: Paste this script into the online terminal and run it — no install needed.

Variant to try

To fill between a curve and the x axis instead, drop the third column and name the axis: plot sin(x) with filledcurves x1 title "above/below axis" fills between the curve and y=0, coloring both sides the same. The keywords above and below restrict the fill to one side, for example with filledcurves above x1 shades only where the curve is positive — the standard way to highlight gains versus losses in one series.

Related tutorials: Line Styles, Colors and Point Types, Your First Gnuplot Plot. See more copy-and-run scripts in the Gnuplot Examples index.

Scripts on this page are adapted from the official gnuplot 6.0 demo collection (gnuplot.info) and rewritten for this tutorial.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *