Gnuplot plot specific lines from data file - gnuplot

I have a data file with 24 lines and 3 columns. How can I plot only the data from specific lines, e.g. 3,7,9,14,18,21?
Now I use the following command
plot 'xy.dat' using 0:2:3:xticlabels(1) with boxerrorbars ls 2
which plots all 24 lines.
I tried the every command but couldn't figure out a way that works.

Untested, but something like this
plot "<(sed -n -e 3p -e 7p -e 9p xy.dat)" using ...
Another option may be to annotate your datafile, if as it seems, it contains multiple datasets. Let's say you created your datafile like this:
1 2 3
2 1 3 # SetA
2 7 3 # SetB
2 2 1 # SetA SetB SetC
Then if you wanted just SetA you would use this sed command in the plot statement
sed -ne '/SetA/s/#.*//p' xy.dat
2 1 3
2 2 1
That says..."in general, don't print anything (-n), but, if you do see a line containing SetA, delete the hash sign and everything after it and print the line".
or if you wanted SetB, you would use
sed -ne '/SetB/s/#.*//p' xy.dat
2 7 3
2 2 1
or if you wanted the whole data file, but stripped of our comments
sed -e 's/#.*//' xy.dat
If you wanted SetB and SetC, use
sed -ne '/Set[BC]/s/#.*//p' xy.dat
2 7 3
2 2 1

If the lines you want have something in common that you can evaluate, e.g. the label in column 1 begins with an "a"
plot dataf using (strcol(1)[1:1] eq "a" ? $0 : NaN):2:xticslabel(1)
you can just skip these lines by letting the using statement return "NaN".
This here is an ugly hack you can use in case the desired line numbers are just arbitrary:
linnum = " 1 3 7 12 16 21 "
plot dataf using (strstrt(linnum," ".int($0)." ") != 0 ? $0 : NaN):2
strstrt(a,b) returns the position of string b in string a, zero if it does not appear. I add the two spaces to make the line numbers unique.
But I would recommend using an external program to preprocess the data in that case, see the other answer.

Yes, there is a solution with every. Since you want to plot with boxerrorbars it can be done in a plot for-loop.
no external tools, i.e. gnuplot-only and hence platform-independent
no strictly increasing line numbers, but arbitrary sequence of lines possible
Script:
### plot only certain lines appearing in a list
reset session
# create some random test data
set print $Data
do for [i=1:24] {
print sprintf("line%02d %g %g", i, rand(0)*5+1, rand(0)*0.5)
}
set print
myLines = "3 7 9 14 18 21"
myLine(i) = int(word(myLines,i)-1)
set offsets 0.5,0.5,0,0
set style fill solid 0.3
set boxwidth 0.6
set xtics out
set key noautotitle
set yrange [0:]
plot for [i=1:words(myLines)] $Data u (i):2:3:xtic(1) \
every ::myLine(i)::myLine(i) w boxerrorbars lc "blue"
### end of script
Result:

Related

Gnuplot: store last data point as variable

I was wondering if there is an easy way, using Gnuplot, to get the last point in a data file say data.txt and store the values in a variable.
From this question,
accessing the nth datapoint in a datafile using gnuplot
I know that I can get the X-Value using stats and GP_VAL_DATA_X_MAX variable, but is there a simple trick to get the corresponding y-value?
A third possibility is to write each ordinate value into the same user variable during plotting. Last value stays in:
plot dataf using 1:(lasty=$2)
print lasty
If you want to use Gnuplot, you can
plot 'data.txt'
plot[GPVAL_DATA_X_MAX:] 'data.txt'
show variable GPVAL_DATA_Y_MAX
OR
plot 'data.txt'
plot[GPVAL_DATA_X_MAX:] 'data.txt'
print GPVAL_DATA_Y_MAX
If you know how your file is organised (separators, trailing empty lines) and you have access to standard Unix tools, you make use of Gnuplot’s system command. For example, if you have no trailing newlines and your values are separated by tabs, you can do the following:
x = system("tail -n 1 data.txt | cut -f 1")
y = system("tail -n 1 data.txt | cut -f 2")
(tail gets the last n lines of a file. cut extracts the column f.)
Note that x and y are strings if obtained this way, but for most applications this should not matter. If you must convert them, you can still add zero.
Let me add a 4th solution, because:
To be very precise, the OP asked about the last x-value and the correscponding y-value.
#TomSolid's solution will return the maximum x-value and its corresponding y-value.
However, strictly speaking the maximum value might not necessarily be the last value, unless the x-data is sorted in ascending order.
The result for the example below would be 10 and 14 instead of 8 and 18.
#Karl's solution will return the last y-value and as well plots something, although you maybe just want to extract the value and plot something else. Ideally, you could combine extraction and plotting.
#Wrzlprmft's solution is using the Linux function tail which is not platform-independent (for Windows you first would have to install such utilities)
Hence, here is a solution:
platform-independent and gnuplot-only
returns the last x-value and corresponding y-value
doesn't create any dummy plot
Script:
### get the last x-value and corresponding y-value
reset session
$Data <<EOD
1 11
2 12
3 13
10 14
5 15
6 16
7 17
8 18
EOD
stats $Data u (lastX=$1,lastY=$2) nooutput
print lastX, lastY
### end of script
Result:
8.0 18.0

Creating a nested for loop in bash script

I am trying to create a nested for loop which will count from 1 to 10 and the second or nested loop will count from 1 to 5.
for ((i=1;i<11;i++));
do
for ((a=1;a<6;a++));
do
echo $i:$a
done
done
What I though the output of this loop was going to be was:
1:1
2:2
3:3
4:4
5:5
6:1
7:2
8:3
9:4
10:5
But instead the output was
1:1
1:2
1:3
1:4
1:5
2:1
...
2:5
3:1
...
and same thing till 10:5
How can I modify my loop to get the result I want!
Thanks
Your logic is wrong. You don't want a nested loop at all. Try this:
for ((i=1;i<11;++i)); do
echo "$i:$((i-5*((i-1)/5)))"
done
This uses integer division to subtract the right number of multiples of 5 from the value of $i:
when $i is between 1 and 5, (i-1)/5 is 0, so nothing is subtracted
when $i is between 6 and 10, (i-1)/5 is 1, so 5 is subtracted
etc.
You must not use a nested loop in this case. What you have is a second co-variable, i.e. something that increments similar to the outer loop variable. It's not at all independent of the outer loop variable.
That means you can calculate the value of a from i:
for ((i=1;i<11;i++)); do
((a=((i-1)%5)+1))
echo $i:$a
done
%5 will make sure that the value is always between 0 and 4. That means we need i to run from 0 to 9 which gives us i-1. Afterwards, we need to move 0...4 to 0...5 with +1.
I know #AaronDigulla's answer is what OP wants. this is another way to get the output :)
paste -d':' <(seq 10) <(seq 5;seq 5)
a=0
for ((i=1;i<11;i++))
do
a=$((a%5))
((a++))
echo $i:$a
done
If you really need it to use 2 loops, try
for ((i=1;i<3;i++))
do
for ((a=1;a<6;a++))
do
echo "$((i*a)):$a"
done
done

fitting a function with multiple data sets using gnuplot

I would like to fit a function using many data sets. For example, I reproduce an experience many times, each time I obtain a pair of data column (x,y). I put all these column in a file named 'data.txt' :
first experience : x = column 1, y = column 2
second experience : x = column 3, y = column 4
third experience : x = column 5, y = column 6
...
Now I wish to fit a function y = f(x) for these data sets. I do not know if Gnuplot can do that ? If it is possible, could you please help me to correct the following command ? This one does not work.
fit f(x) "data.txt" u 1:2:(0.25), "data.txt" u 3:4:(0.25), "data.txt" u 5:6:(0.25) via a, b
You can process your data so that columns 1, 3 and 5 all become the same column 1, and columns 2, 4 and 6 all become the same column 2. It's easy with awk, you can do it outside gnuplot:
awk '{print $1, $2} {print $3, $4} {print $5, $6}' data.txt > data2.txt
and then fit it within gnuplot:
f(x)=a*x+b
fit f(x) "data2.txt" u 1:2:(0.25) via a,b
Or you can do it completely within gnuplot without any intermediate file:
f(x)=a*x+b
fit f(x) "< awk '{print $1, $2} {print $3, $4} {print $5, $6}' data.txt" u 1:2:(0.25) via a,b

column with empty datapoints

date daily weekly monthly
1 11 88
2 12
3 45 44
4 54
5 45
6 45 66
7 77
8 78
9 71 99 88
For empty data points in weekly column , the plot is ploting values from monthly column.
Monthly column plot and daily column plot are perfect.
suggest something more than set datafile missing ' ' and set datafile separator "\t"
Alas, Gnuplot doesn't support field based data files, the only current solution is is to preprocess the file. awk is well suited for the task (note if the file contains hard tabs you need to adjust FIELDWIDTHS):
awk '$3 ~ /^ *$/ { $3 = "?" } $4 ~ /^ *$/ { $4 = "?" } 1' FIELDWIDTHS='6 7 8 7' infile > outfile
This replaces empty fields (/^ *$/) in column 3 and 4 with question marks, which means undefined to Gnuplot. The 1 at the end of the awk script invokes the default rule: { print $0 }.
If you send awk's output to outfile, you can for example now plot the file like this:
set key autotitle columnhead out
set style data linespoint
plot 'outfile' using 1:2, '' using 1:3, '' using 1:4
If anyone runs into this, I recommend updating to at least the 4.6.5 Gnuplot version.
This is because from Gnuplot 4.6.4 update:
* CHANGE treat empty fields in a csv file as "missing" rather than "bad"
And there seemed to be a (related?) bugfix in 4.6.5:
* FIX empty first field in a tab-separated-values file was incorrectly ignored

plot if col A has substring

I need to do this in gnuplot:
plot 1:4 where col 2=="P1", col 3=="3", col 1 has substring "blur1"
Heres a dataset:
col_1 col_2 col_3 col_4
gcc.blur1.O0 P1 3 10.5
icc.blur1.O2 P2 5 9.8
gcc.blur2.O3 P2 3 8.9
Thanks in advance.
AFAIK you need to use an external script to check for substrings. Something like awk and use
plot "< awk '{...awk commands...}' input.dat"
If you just want to test col_2 for P1 you can do it in gnuplot via
f(x,y)= (x eq "P1"? y : 1/0)
plot "input.dat" u 3:(f(strcol(2),$4))
strcol(n) gets the n-ths column as a string. "eq" can be used to compare strings.
Such a simple check can be done with gnuplot's strstrt function. It returns the index of the first character found, or 0 if the substring wasn't found:
strstrt(strcol(3), 'blur1') > 0
So, your plot command can look like
matchesLine(col1, col2, col3) = strstrt(strcol(1), col1) > 0 && strcol(2) eq col2 && strcol(3) eq col3
plot 'file' using (matchesLine("blur1", "P1", "3") ? $1 : 1/0):4
If you know the exact position of the substring in advance, you can try to check for equality on that portion like this:
strcol(1)[5:9] eq 'blur1'
ps.: Here are some handy examples for dealing with strings in gnuplot:
http://gnuplot.sourceforge.net/demo/stringvar.html

Resources