merging multiple data files to plot in a single graph - gnuplot

extending the question I had asked earlier which can be found here, plotting multiple (x,y) co-ordinates in a single curve with gnuplot. I am trying to plot the bezier curves in gnuplot using 2 different files. Each (x,y) from the first file forms a bezier curve passing through the points from the second file. The first file has the co-ordinates like the following:
x y
0.0 0.5
0.12 0.1
0.16 0.4
0.2 0.35
0.31 0.8
0.34 0.6
0.38 1.0
0.46 0.2
0.51 0.7
0.69 0.9
The second file has the co-ordinates as the following:
x y
0.00 0.7
0.04 0.74073082208
0.08 0.85926917792
0.12 0.9
0.16 0.9
0.2 0.9
0.24 0.749720623086
0.28 0.874229601255
0.32 0.74073082208
0.36 0.8
0.4 0.721178508605
0.44 0.878821491395
0.48 0.761772990545
0.52 0.700774803388
0.56 0.723771273415
0.6 0.789508073675
0.64 0.864014272269
0.68 0.896743348931
Now, how do I merge these two files to plot a single graph. These two files don't have the same number of rows, but I guess that doesn't matter. The first curve would be between (x1,y1) and (x2,y2) from the first file which would continue till (x10,y10). In the curve between (x1,y1) and (x2,y2); the points (x1,y1), (x2,y2) and (x3,y3) from the second file lie.
I followed this link http://t16web.lanl.gov/Kawano/gnuplot/datafile3-e.html to sort and concatenate the two files but got some weird lines which is totally wrong. These values should actually plot Bezier curves but not getting the graph. I have written the following gnuplot script to plot the concatenated data:
set term x11 persist
set title "Animation curves"
set xlabel "Time (secs.)"
set ylabel "Parameter"
set x2label "Phoneme1" offset -35
set pointsize 2
set key off
set style line 2 lt 0 lc 1 lw 2
set xrange [0.0:1.0]
set yrange [0.0:1.3]
plot [0.0:0.8] "< cat -n file1.dat" u 1:2 smooth csplines ls 1, "" u 1:(0.0):(0):(1.3) w vectors nohead ls 2, "" u ($1+0.005):(1):(sprintf("P %d", $0)) w labels, \
"file1.dat" u 1:2 with points, \
"file2.dat" u 1:2 with points, \
I got the following error:
plot "< cat -n file1.dat" u 1:2 smooth csplines ls 1, "" u 1:(0.0):(0):(1.3) w vectors nohead ls 2, "" u ($1+0.005):(1):(sprintf("P %d", $0)) w labels, "file1.dat" u 1:2 with points, "file2.dat" u 1:2 with points,
^
"plot.gp", line 21: Cannot smooth: no data within fixed xrange!

The script below works on my machine. Maybe this is even what you are looking for...
set term x11 persist
set title "Animation curves"
set xlabel "Time (secs.)"
set ylabel "Parameter"
set x2label "Phoneme1" offset -35
set pointsize 2
set key off
set style line 2 lt 0 lc 1 lw 2
set xrange [0.0:1.0]
set yrange [0.0:1.3]
plot [0.0:0.8] "< cat file1.dat file2.dat | sort -n" u 1:2 smooth csplines ls 1, \
"" u 1:(0):(0):2 w vectors nohead ls 2, \
"" u ($1 + 0.005):(1):(sprintf("P %d", $0)) w labels, \
"file1.dat" u 1:2 w p, \
"file2.dat" u 1:2 w p

Related

Fill colour between two smooth lines drawn by data points in gnuplot

I have file “data.txt” containing three columns. Column 1 is for x axis. I want to draw smooth curves corresponding to data points of column 2 and 3 and then want to fill colour between these two lines.
File content is;
10 -1.3 1.1
20 -0.956 0.933
50 -0.761 0.684
80 -0.523 0.439
110 -0.227 0.20
130 -0.07 0.06
My script lines are,
plot “data.txt” u 1:2 smooth bezier w filledcurves above,\
“data.txt” u 1:3 smooth bezier w filledcurves below
But I’m not getting desired shaded plot.
I guess you have two challenges here:
you cannot smooth and fill at the same time
you can only fill between two columns of the same dataset or file
One possible way would be the following:
plot the data smooth bezier into a table $Smooth
N is the number of lines the combined smoothed data $Smooth
merge these smoothed data by using line 1 and 1+N/2, line 2 and 2+N/2, etc. into a dataset $Paste
for the fill you then have to use columns 1, 2 and 5
Script:
### fill area between smoothed curves
reset session
$Data <<EOD
0 0 90
10 10 50
50 20 40
80 50 60
100 30 50
EOD
set table $Smooth
set samples 20
plot $Data u 1:2 smooth bezier
plot $Data u 1:3 smooth bezier
unset table
set print $Paste
N = |$Smooth|
do for [i=1:N/2] {
print $Smooth[i].$Smooth[i+N/2]
}
set print
plot $Data u 1:2:3 w filledcurves lc rgb 0xcc0000ff ti "fill between data", \
'' u 1:2 w lp pt 7 lc "red" ti "original data", \
'' u 1:3 w lp pt 7 lc "red" notitle, \
$Smooth u 1:2 w lp pt 7 lc "green" ti "smoothed curves", \
$Paste u 1:2:5 w filledcurves lc rgb 0xccff0000 ti "fill between smoothed"
### end of script
Result:
For future reference, the development version of gnuplot (5.5) greatly extends the options for smoothing. For your case the obvious command works as expected. The only caveat is that for open curves (endpoints are not equal) you must use smooth sbezier rather than smooth bezier.
$DATA << EOD
10 -1.3 1.1
20 -0.956 0.933
50 -0.761 0.684
80 -0.523 0.439
110 -0.227 0.20
130 -0.07 0.06
EOD
set xrange noextend
set style fill transparent solid 0.25
plot $DATA using 1:2:3 smooth sbezier with filledcurves between, \
'' using 1:2 with lp, '' using 1:3 with lp
As far as I understand, above and below expect three column input, and this does not work with smooth.
But starting from this and this answer, you can try something like the following:
set xzeroaxis
set tics front
plot "data.txt" u 1:3 smooth bezier notitle w filledcurves x1 lc rgb "#b0ffff00", \
"data.txt" u 1:2 smooth bezier notitle w filledcurves x1 lc rgb "#00ffffff", \
"data.txt" u 1:3 smooth bezier lt 1, \
"data.txt" u 1:2 smooth bezier lt 2
Both filledcurves plots reach from the curve to the x-axis, the second one in white covers the first one in yellow leaving the space between the two curves in yellow.
See help lc or help linecolor for color details.
This is the result:

How to plot simple line segments in GNUPlot?

I am trying to draw a rudimentary line segment from (0,0) to (0,1). I already have an input file, but I want to add a line to it.
I have read through Line plot in GnuPlot where line width is a third column in my data file? and Plot line segments from a datafile with gnuplot among many other examples. Everyone is doing something much more complex than what I want, I only want the line segment added to my GNUPlot script.
I normally enter the data in to GNUPlot thus:
$DATA << EOD
.... other data
EOD
$LS << EOL
0 0
0 1
EOL
plot $DATA using 1:2:3 with points
plot $LS with lines
but this doesn't work, nor does
plot $LS using 1:2 with lines
How can I plot this simple line segment from (0,0) to (0,1)?
What does "doesn't work" mean? You don't even show the resulting graph. By the way what do you use the 3rd column for?
Your first example will make a plot only with your data and then a new plot with only the line segment.
And in your second example, the line from 0,0 to 0,1 in your plot is identical with the y-axis and therefore hard to see as long as the xrange starts from 0. You can easily check this, e.g. if you set the color to red or linewidth to 3 , e.g.
plot $LS u 1:2 lc "red" lw 3
You also can make your line segment "visible" if you set xrange[-0.2:]. Check the following example
Code:
### plotting simple line segments
reset session
$DATA << EOD
0.1 0.2 1
0.3 0.4 2
0.5 0.6 3
0.7 0.5 4
EOD
$LS1 << EOL
0 0
0 1
EOL
$LS2 <<EOL
0 0
1 1
EOL
set xrange [-0.2:]
plot $DATA using 1:2:3 with points pt 7, \
$LS1 u 1:2 with lines, \
$LS2 u 1:2 with lines
### end of code
Result:
You can use headless arrows to plot arbitrary line segments.
set xrange [0:1]
set yrange [0:1]
set arrow 1 from 0,0 to 0,1 nohead lw 3 lc 2
set arrow 2 from 0,0 to 1,1 nohead lw 2
plot NaN t''

Gnuplot: How to draw an opposing bar plot or pyramid bar diagram

Is it possible to use two positive y-axes with gnuplot? This is just a simple example for the question.
plot.gp:
reset
set style fill solid 1
set boxwidth 0.8 relative
plot 'data1.dat' index 1 using 1:2 with boxes title 'A' ,\
'' index 2 using 1:(-$2) with boxes title 'B'
Instead of using 1:(-$2) I would like to use 1:2 in the last line in plot.gp.
data1.dat:
0.12 0.024
0.15 0.132
0.18 0.241
0.22 0.136
0.12 0.039
0.15 0.219
0.18 0.197
0.22 0.155
From:
To:
Same answer as the one from theoz except that the y2 labels are shifted to the left side of the plot and the set link y2 command is used to generalize the axis inversion.
You can tweak the offset graph -1.03 to superimpose the "0" labels, then delete the duplicate "0" label by changing the y2 tic range to set y2tics 0.1,0.1.
$Data <<EOD
0.12 0.024
0.15 0.132
0.18 0.241
0.22 0.136
0.12 0.039
0.15 0.219
0.18 0.197
0.22 0.155
EOD
set style fill transparent solid 0.5
set boxwidth 0.8 relative
set xzeroaxis ls -1
set yrange[-0.3:0.3]
set ytics 0,0.1
set mytics 2
# Set y2 axis to exact mirror of y1
# Shift tic labels to the left and use right-justified text
set link y2 via -y inv -y
set y2tics 0,0.1
set y2tics offset graph -1.03 right
plot $Data u 1:2 index 0 axis x1y1 w boxes title 'A' ,\
'' u 1:2 index 1 axis x1y2 w boxes title 'B'
Here is a solution with a single plot, however, the y-axis tics are on different sides.
Maybe there is some easy way to get them on the same side.
Code:
### two "positive" y-axes
reset session
$Data <<EOD
0.12 0.024
0.15 0.132
0.18 0.241
0.22 0.136
0.12 0.039
0.15 0.219
0.18 0.197
0.22 0.155
EOD
set style fill transparent solid 0.5
set boxwidth 0.8 relative
set yrange[-0.3:0.3]
set ytics 0,0.1
set mytics 2
set y2range[0.3:-0.3]
set y2tics 0,0.1
set my2tics 2
set y2tics mirror
set xzeroaxis ls -1
plot $Data u 1:2 index 0 axis x1y1 w boxes title 'A' ,\
'' u 1:2 index 1 axis x1y2 w boxes title 'B'
### end of code
Result:
Addition:
Here is a second suggestion with all ytic labels on one side.
A little drawback is that it is not autoscale and you have to set the range and the steps "manually".
It is using the command evaluate (check help evaluate). And real() (check help real) in order to avoid gnuplot's integer division in case the two numbers of the division might be integers.
Code:
### y-axis with two "positive" directions
reset session
$Data <<EOD
0.12 0.024
0.15 0.132
0.18 0.241
0.22 0.136
0.12 0.039
0.15 0.219
0.18 0.197
0.22 0.155
EOD
set style fill transparent solid 0.5
set boxwidth 0.8 relative
set xzeroaxis ls -1
Max = 0.3
Steps = 3
set yrange[-Max:Max]
set ytics () # remove all tics
do for [i=-Steps:Steps] {
myTic = sprintf('set ytics add ("%g" %g)',abs(real(i)/Steps*Max),real(i)/Steps*Max)
eval(myTic)
}
plot $Data u 1:2 index 0 axis x1y1 w boxes lc 3 title 'A' ,\
'' u 1:(-$2) index 1 axis x1y1 w boxes lc 4 title 'B'
### end of code
Result:

gnuplot's 'steps' style does not accept variable color

I'm using Gnuplot Version 5.2 patchlevel 6 on Debian 10. The following program
$d << EOD
1 0.5 0.1
2 0.75 0.2
3 0.99 0.5
4 1.25 1.1
EOD
plot $d using 1:2:3 w lines lc palette z lw 2
produces an expected output:
But if I change the last line to
plot $d using 1:2:3 w steps lc palette z lw 2
I receive an error message:
line 7: Too many using specs for this style
According to paragraphs II Plotting Styles, Steps in Gnuplot User Manual
The input column requires are the same as for plot styles lines and points.
and in paragraph II Plotting Styles, Lines stated that:
The basic form requires 1, 2, or 3 columns of input data. Additional input columns may be used to provide information such as variable line color
What am I doing wrong?
If you are drawing with steps, the question probably is: which color should the vertical lines have?
Quickly checking the documentation I couldn't find a hint whether variable line color together with steps explicitely works or explicitely doesn't work.
In any case, you can workaround with the following code:
Code:
### plotting with steps and variable line color
reset session
$Data <<EOD
1 0.5 0.1
2 0.75 0.2
3 0.99 0.5
4 1.25 1.1
EOD
set xrange [0:5]
set yrange [0:1.5]
plot x1=y1=NaN $Data u (x0=x1,x1=$1,x0):(y0=y1,y1=$2,y0):(x1-x0):(0):3 w vectors lw 2 lc palette nohead notitle, \
x1=y1=NaN $Data u (x0=x1,x1=$1,x1):(y0=y1,y1=$2,y0):(0):(y1-y0):3 w vectors lw 2 lc palette nohead notitle
### end of code
Result:
Addition: (vertical lines with variable colors)
Maybe you noticed that with your 4 datapoints there are only 3 colors. This is obvious, because if you have 4 data points you will only have 3 connecting lines, hence 3 colors.
A variation would be the following:
Draw your 4 points with the color according to the value column 3 and the same color for the horizontal lines.
However, for the vertical lines you split the lines into as many levels you want (here: myLevels = 20) using the color according to the palette.
Code:
### plotting with steps and variable line color (vertical lines with variable color)
reset session
$Data <<EOD
1 0.5 0.1
2 0.75 0.2
3 0.99 0.5
4 1.25 1.1
EOD
set xrange [0:5]
set yrange [0:1.5]
myLevels = 20
plot x1=y1=c1=NaN $Data u (x0=x1,x1=$1,x0):(y0=y1,y1=$2,y0):(x1-x0):(0):(c0=c1,c1=$3,c0) w vectors lw 2 lc palette nohead notitle, \
for [i=0:myLevels-1] x1=y1=NaN $Data u (x0=x1,x1=$1,x1):(y0=y1,y1=$2,y0+(y1-y0)*i/myLevels):(0):((y1-y0)/myLevels):(c0=c1,c1=$3,c0+(c1-c0)*i/myLevels) w vectors lw 2 lc palette nohead notitle, \
$Data u 1:2:3 w p pt 7 ps 2 lc palette notitle
### end of code
Result:

gnuplot - calculate distance between lines

Can gnuplot calculate the distance between two lines or maybe two points?
I'm having a plot where two (main) lines are plotted. For the moment let's assume that the first line is always above the second one. Is there a way to calculate the distance from line 2 to line 1 at a given x-value?
here is a picture of what my plot looks like and which distance I want to calculate:
The vertical lines are just for style and have nothing to do with the actual plot, their data is stored in test.dat and test2.dat.
My data-files of the lines look like this:
line1
0 118.1
2.754 117.77
4.054 117.64
6.131 116.17
7.7 116.04
8.391 115.36
10.535 115.25
11.433 116.03
12.591 116.22
19.519 118.59
line2
19.4 118.51
15.2 116.56
10.9 115.94
10.35 114.93
9.05 114.92
8.3 115.9
5.9 116.19
4.2 116.62
2.2 117.66
-0.3 118.06
My plotting-code looks like this:
set term wxt enhanced
cd 'working directory'
unset key
set size 0.9,0.9
set origin 0.1,0.1
set title 'TITLE'
unset border
set label 21 " rotate by 45" at -3.0,0.0 rotate by 45 point ps 2
set xrange [0:19.519]
set yrange [110:119]
set xtics nomirror(0, 2.745, 4.054, 6.131, 7.7, 8.391, 10.535, 11.433, 12.591, 19.519) rotate by 90 offset 0,-0.1 right
set ytics " ", 30000
plot "line1.dat" using ($1):($2):2 with labels offset 1, 1.8 rotate by 90, "line1.dat" using 1:2 with lines lt 1 lw 1 lc rgb '#000000', +112 lt 1 lw 1 lc rgb '#000000' , 'test.dat' with lines lt 1 lw 1 lc rgb '#000000', +110 lt 1 lw 1 lc rgb '#000000', 'line2.dat' with lines lt 0.5 lw 1 lc rgb '#000000', 'test2.dat' with lines lt 0.5 lw 1 lc rgb '#000000'
You can measure the distance manually. Move the mouse to the first point and type 'r'. Then as you move the mouse around, the x and y offsets, distance and angle are displayed. Type '5' to draw a line segment and to toggle between degrees and tangent display. Zooming in beforehand increases accuracy.
By the way, typing 'h' in the plot window will display a list of keybindings to the console.
An answer to this "rather old" question still might be of interest to OP, if not, maybe to others.
Yes, you can calculate and plot the difference of two lines. It requires some linear interpolation. Simply assign the desired x-value to the variable myX.
Data:
SO17717287_1.dat
0 118.1
2.754 117.77
4.054 117.64
6.131 116.17
7.7 116.04
8.391 115.36
10.535 115.25
11.433 116.03
12.591 116.22
19.519 118.59
SO17717287_2.dat
19.4 118.51
15.2 116.56
10.9 115.94
10.35 114.93
9.05 114.92
8.3 115.9
5.9 116.19
4.2 116.62
2.2 117.66
-0.3 118.06
Script: (works for gnuplot>=4.6.0)
### calculating and plotting a difference between two curves
reset
FILE1 = "SO17717287_1.dat"
FILE2 = "SO17717287_2.dat"
set border 1
unset key
set origin 0.05,0.05
set size 0.9,0.8
set xrange [0:19.519]
set xtics nomirror rotate by 90 offset 0,-0.1 right
set yrange [110:119]
unset ytics
myX = 15.2
getYa(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? ya=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? ya=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
getYb(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? yb=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? yb=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
set samples 2 # set to minimal possible value for plotting '+'
plot x1=y1=NaN FILE1 u 1:2:2:xtic(1) w labels offset 0,0.5 left rotate by 90, \
'' u 1:(getYa(myX),$2) w l lc rgb 'black', \
'' u 1:2:(0):(110-$2) w vec lt 0 nohead, \
+112 w l lc rgb 'black', \
x1=y1=NaN FILE2 u 1:(getYb(myX),$2) w l lt 0 lc rgb 'black', \
'+' u (myX):(ya):(0):(yb-ya) w vec heads lc rgb "red", \
'+' u (myX):(ya):(sprintf("%.3f",yb-ya)):xtic(sprintf("%g",myX)) w labels tc rgb "red" offset 0,1, \
'+' u (myX):(ya):(0):(110-ya) w vec nohead lt 0 lc rgb "red"
### end of script
Result: (created with gnuplot 4.6.0)

Resources