0

I have a file composed of 3 columns, which format is: X Axis Value | Title ID | Y axis Value

I don´t know how to plot it using columns 1:3 and 2 for the title name. Here is an example:

X Axis  Plot  Y Axis
2000    plot1 1.2
2000    plot2 4.6
2000    plot3 5.7
3000    plot1 5.8
3000    plot2 7.5
3000    plot3 8.3

So here, we will have 3 plots which should have second column values on their names (1, 2 and 3); 2000 and 3000 would be the X axis values and the thrid column represents de Y value.

So, the "title-2" graph would be: (2000, 4.6) and (3000, 7.5)

2
  • Welcome to StackOverflow. Check this: stackoverflow.com/a/62498110/7295599 it's pretty similar. If you have difficulties to adapt it to your needs, modify your question and describe the exact problem. Commented Dec 7, 2020 at 20:15
  • Yes, it is very similar! But I don´t get the way to adapt it. I have modified the data to display. There would be 3 plots, named plot1, plot2 and plot3. Commented Dec 7, 2020 at 22:32

2 Answers 2

0

If you are a UNIX user or the "grep" command is callable from gnuplot, you may consider the following approach.

set xrange [1000:4000]
set yrange [0:10]

plot for [i=1:3] \
        sprintf("< grep plot%i test.dat",i) using 1:3 with linespoints title sprintf("plot%i", i)

The above plot command is a bit more complicated because of the use of "for loop" and "sprintf", but is equivalent to the following.

plot "< grep plot1 test.dat" using 1:3 with linespoints title "plot1", \
     "< grep plot2 test.dat" using 1:3 with linespoints title "plot2", \
     "< grep plot3 test.dat" using 1:3 with linespoints title "plot3"
Sign up to request clarification or add additional context in comments.

Comments

0

The adapted "gnuplot only" version for your case might look like this. fcol is the filter column number, myKey your keyword, and dcol the data column number. It's not clear to me whether you want 3 lines in 1 graph, or 3 lines in 3 graphs on 1 canvas (multiplot, like example below), or the graph in 1 file on disk or maybe distributed on 3 files on disk.

Code:

### split data by keyword for each plot
reset session

$Data <<EOD
X Axis  Plot    Y Axis
2000    plot1   1.2
2000    plot2   4.6
2000    plot3   5.7
3000    plot1   5.8
3000    plot2   7.5
3000    plot3   8.3
EOD

myFilter(fcol,myKey,dcol) = strcol(fcol) eq myKey ? column(dcol) : NaN
set datafile missing NaN
set key top left

set multiplot layout 3,1
    do for [i=1:3] {
        myKey = sprintf("plot%d",i)
        set title myKey
        plot $Data u 1:(myFilter(2,myKey,3)) w lp pt 7 lc i title myKey
    }
unset multiplot
### end of code

Result:

enter image description here

Addition: (all lines in one plot)

If you have regular file pattern in your rows, e.g. 1,2,3,1,2,3,1,2,3,... or 1,1,1,2,2,2,3,3,3,... you possibly could also work with every, check help every. However, this filtering with the ternary operator should work for all cases, including random 1,3,2,2,3,1,1,2,3,... sequences.

Code:

### split data by keyword
reset session

$Data <<EOD
X Axis  Plot    Y Axis
2000    plot1   1.2
2000    plot2   4.6
2000    plot3   5.7
3000    plot1   5.8
3000    plot2   7.5
3000    plot3   8.3
EOD

myFilter(fcol,myKey,dcol) = strcol(fcol) eq myKey ? column(dcol) : NaN
set datafile missing NaN
set key top left

myKey(i) = sprintf("plot%d",i)
set title "Plot 1,2,3"
plot for [i=1:3] $Data u 1:(myFilter(2,myKey(i),3)) w lp pt 7 lc i title myKey(i)
### end of code

Result:

enter image description here

2 Comments

But, is there a way to have this three plots in the same graphic?
yes, of course! I will add it to the answer.

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.