rfeynman
rfeynman

Reputation: 121

How to skip missing data in gnuplot

I have the following data:

1  2  3  4
1  2  3  4
1  2     4
1  2     4

If I use

plot "file" u 1:3

then it plots {1,3},{1,3},{1,4},{1,4}

How do I plot it following the column?

This is a txt file.

Upvotes: 1

Views: 382

Answers (2)

Ethan
Ethan

Reputation: 15093

If your data is exactly as shown (single character in first column, three characters in columns two through four) then it is possible to extract each column from the string on input using the following trick.

The first real data column is seen as strcol(1)[1:1] The second is seen as strcol(1)[2:4] The third is seen as strcol(1)[5:7]

This allows a command sequence

$DATA << EOD
1  2  3  4
1  2  3  4
1  2     4
1  2     4
1  2  7  8
EOD

# There are no commas, so the program will treat the entire line a "column 1"
set datafile separator comma

# Treat blank column as NaN, otherwise convert to integer
convert(s) = (s eq "   " ? NaN : int(s))

# For the sake of example, just print the result rather than plotting
set table

plot $DATA using (convert(strcol(1)[1:1]) : (convert(strcol(1)[5:7]) with points

Output:

# Curve 0 of 1, 5 points
# Curve title: "$DATA using (convert(strcol(1)[1:1])):(convert(strcol(1)[5:7]))"
# x y type
 1  3  i
 1  3  i
 1  NaN  u
 1  NaN  u
 1  7  i

If you issue the command without the set table it will produce a graph with three points. The NaN points will be omitted.

Upvotes: 1

theozh
theozh

Reputation: 25694

gnuplot's standard column separator is whitespace and does not distinguish between a single space and multiple spaces. Check help datafile separator. If your column separator is strictly one and only one space you can simply set datafile separator " ".

However, then your data must look like this:

1 2.1 3.1 4.1
2 2.2 3.2 4.2
3 2.3  4.3      # two spaces but not more
4 2.4  4.4      # ditto
5 2.5 3.5 4.5

But since your data doesn't seem to look like this, you probably have to go for this workaround.

Nevertheless, here is the first option.

Script:

### empty columns
reset session

$Data <<EOD
1 2.1 3.1 4.1
2 2.2 3.2 4.2
3 2.3  4.3
4 2.4  4.4
5 2.5 3.5 4.5
EOD

set key out tmargin

set multiplot layout 1,2

    set datafile separator whitespace    # this is default
    plot $Data u 1:2 w lp pt 7 lc "red", \
           ''  u 1:3 w lp pt 7 lc "green", \
           ''  u 1:4 w lp pt 7 lc "blue"

    set datafile separator " "
    plot $Data u 1:2 w lp pt 7 lc "red", \
           ''  u 1:3 w lp pt 7 lc "green", \
           ''  u 1:4 w lp pt 7 lc "blue"

unset multiplot
### end of script

Result:

enter image description here

Upvotes: 2

Related Questions