In gnuplot, how to plot with lines but skip missing data points?

Put an empty record (blank line) where there is no data. From the docs:

Single blank records designate discontinuities in a plot; no line will join points separated by a blank records (if they are plotted with a line style).


You can use any string that is not a number as value for the missing data points or explicitly specify a missing data string using the set datafile missing command.

If you then plot the lines using

plot "vikt_ma.txt" using 1:($2) with lines title "first line"

then Gnuplot will leave a gap.


You can also do something like this to automatically create gaps when the distance between x values exceeds some threshold:

previous=1
current=1
shift(x) = (previous=current, current=x)
yornothing(x,y) = ( shift(x), abs(x-previous)<7200?y:sqrt(0/0))

plot "file.dat" using 1:(yornothing($1,$2)) with lines

You'll need to adjust the initial values of "previous" and "current", and the threshold ("7200" in the example above).

The function "yornothing" uses the function "shift" to store one previous value of x. Each time yornothing is called, it returns either the value of y or "0/0", depending on whether the absolute value of the difference between x and its previous value exceeds the threshold.

A value of 0/0 tells gnuplot to ignore that point.

Tags:

Gnuplot