1

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.

4
  • Apparently your column separator is space. gnuplot doesn't distinguish between single space and multiple spaces. Is this all your data? Do you have a fixed column width? What if you have numbers larger than 10 or floating point numbers? Does your missing data in "column 3" consist of a single and only a single space? Commented Nov 30, 2022 at 18:38
  • Please add a more general (realistic) data example to demonstrate what you consider as a column, e.g. numbers of different length or could there be several (non-)adjacent empty columns in one row? Only then we should start to think about how gnuplot potentially could extract the correct "columns". Commented Dec 1, 2022 at 7:44
  • Problem solved? Any useful answer = upvote. Any answer solving your problem = accept. Commented Oct 20, 2023 at 10:03
  • Yes. It was solved by your first answer. I upvoted. Thank you! Commented Dec 9, 2023 at 2:19

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.