Gnuplot 1d plot - gnuplot

I have a text file of the position of particles along the x axis which changes after every collision. Example data.
0 7.5 10 30 30 40
0 9.375 10 32.5 40 40
0 10 10 33.3333 36.6667 40
0 10.25 10.75 34 34 40
0 11.0938 13.2812 28.75 40 40
I am currently trying to plot the data using gnu plot. What I want it to do is have these points along the x axis but instead of plotting the whole file at once I would like gnu plot to plot one line at a time . Furthermore, so the data is identifiable I am trying to plot the points as large markers instead of points. I am struggling to do this and any help would be appreciated.

Firstly, convert the rows to columns using AWK
awk '{for(i=1;i<=NF;i++)a[NR,i]=$i}END{for(i=1;i<=NF;i++){for(j=1;j<=NR;j++)printf a[j,i]"\t";printf "\n"}}' original.dat > particle.dat
#suppose that your input data is original.dat and the output data is particle.dat
The converted data are:
0 0 0 0 0
7.5 9.375 10 10.25 11.0938
10 10 10 10.75 13.2812
30 32.5 33.3333 34 28.75
30 40 36.6667 34 40
40 40 40 40 40
Then, plot your data with the following code in gnuplot:
set border 1
#`set border 1` means only showing the bottom border of the plot. see `help border` for more information
set xtics nomirror
#only show the bottom tics on the x axis and suppress the upper tics of the x axis
unset ytics
#suppress the tics on the y axis
set key outside
#set the legend out side the plot
plot "particle.dat" using 1:(1) with points pointtype 7 pointsize 3 title "particle 1", "" u 2:(2) w p pt 7 ps 3 t "particle 2", "" u 3:(3) w p pt 7 ps 3 t "particle 3", "" u 4:(4) w p pt 7 ps 3 t "particle 4", "" u 5:(5) w p pt 7 ps 3 t "particle 5"
#`using 1:(1)` means use the first column as X and a constant number of 1 as Y. see `help using` for more details
#`u` is short for `using`and `w p pt 7 ps 3` is short for `with points pointtype 7 pointsize 3.
The output of the plot is

I don't think that you have to transpose the data using awk, as each row already contains the data of a single particle.
So, based on the code from DragonHu, I have this:
To generate this plot, I also added lines to connect the points. Also, I used the special column number 0 which just gives the line number in the datafile, starting at 0.
Another trick: Using backslash \, you can split a command to multiple lines. Here is the plot command I used:
plot "particle.dat" using 1:0 with points linetype 1 pointtype 7 pointsize 3 title "particle 1",\
"" u 1:0 notitle w l lt 1,\
"" u 2:0 w p lt 2 pt 7 ps 3 t "particle 2", \
"" u 2:0 notitle w l lt 2,\
"" u 3:0 w p lt 3 pt 7 ps 3 t "particle 3", \
"" u 3:0 notitle w l lt 3,\
"" u 4:0 w p lt 4 pt 7 ps 3 t "particle 4", \
"" u 4:0 notitle w l lt 4,\
"" u 5:0 w p lt 5 pt 7 ps 3 t "particle 5",\
"" u 5:0 notitle w l lt 5
Still, this is not yet the answer, as the question is to plot one set of points at a time. This can be achieved with the following code. It generates five single plots which I dumped into an animated gif figure:
set key center
set yrange[0:1]
set xrange[0:40]
set terminal gif size 600, 200 animate delay 100
set output "animated.gif"
do for [n=0:4] {
set title sprintf("Lineno. %d", n)
plot "particle.dat" every ::n::n using 1:(0) with points pointtype 7 pointsize 3 title "particle 1",\
"" every ::n::n u 2:(0) w p pt 7 ps 3 t "particle 2", \
"" every ::n::n u 3:(0) w p pt 7 ps 3 t "particle 3", \
"" every ::n::n u 4:(0) w p pt 7 ps 3 t "particle 4", \
"" every ::n::n u 5:(0) w p pt 7 ps 3 t "particle 5",\
}
unset output
If single images should be created, it is possible via
set terminal ongcairo
do for [n=0:4] {
set title sprintf("Lineno. %d", n)
set output sprintf("PictureNumber_%d",n)
plot ...
unset output
}

Related

GNUPLOT : how to stop "histeps" from terminating to zero (y=0) at start and endpoints?

I am using the following script for plotting data points from file "delete.dat"
set terminal jpeg
set output "delete_histeps.jpeg"
set title "with histeps"
plot "delete.dat" using 1:2 index 0 pt 7 ps 0.2 lc "black" notitle,\
"delete.dat" using 1:2 index 1 pt 7 ps 0.2 lc "red" notitle,\
"delete.dat" using 1:2 index 2 pt 7 ps 0.2 lc "green" notitle,\
"delete.dat" using 1:2 index 0 with histeps lc "black",\
"delete.dat" using 1:2 index 1 with histeps lc "red",\
"delete.dat" using 1:2 index 2 with histeps lc "green"
and the "delete.dat" file is
1 2
3 4
5 6
7 8
9 10
-1 5
-2 3
-3 4
-4 2
-5 6
5 1
6 2
7 3
8 4
9 5
10 4
11 3
12 12
But the start and end step is dropping to zero, which I don't want. Though for step and fsteps command, the steps begin (end) at the first (last) data point, without dropping down to zero (y=0). Can you please suggest that works like histeps (which keeps data points in the middle of the steps) but does not terminate to zero (like steps or fsteps). I'm also attaching pictures.
If your data points are equidistant (like the ones you provided), an alternative hack would be possible as well by combining fsteps and steps and shifting the x-value by half the point-to-point-distance:
plot "delete.dat" i 0 pt 7 ps 2 lc "black" not, \
'' i 1 pt 7 ps 2 lc "red" not, \
'' i 2 pt 7 ps 2 lc "green" not, \
'' u ($1-1.0):2 i 0 w steps lc "black", '' u ($1+1.0):2 i 0 w fsteps lc "black" not, \
'' u ($1+0.5):2 i 1 w steps lc "red", '' u ($1-0.5):2 i 1 w fsteps lc "red" not, \
'' u ($1-0.5):2 i 2 w steps lc "green", '' u ($1+0.5):2 i 2 w fsteps lc "green" not
The trick here is that steps or fsteps alone would miss either the last or the first point and the line would end in mid-air, therefore both have to be plotted on top of each other with the same plotting style.
One must take care of the correct sign: if x is positive, steps get a downshift and fsteps get an upshift; and vice-versa if x is negative.
My answer is definitely not as "robust" as the one of theozh, but maybe easier to understand.
Well, that's how hispteps, steps, and fsteps have been defined.
Then, you have to create your own steps. A solution could be the following:
Code: (improved version)
### steps like histeps but not dropping to zero
reset session
set colorsequence classic
$Data <<EOD
1 2
3 4
5 6
7 8
9 10
-1 5
-2 3
-3 4
-4 2
-5 6
5 1
6 2
7 3
8 4
9 5
10 4
11 3
12 12
EOD
set table $myStep
do for [i=0:2] {
plot $Data u ($0==0?(x1=x2=$1):(x1=x2,x2=$1),(c=$0,x1+x2)/2.):2 index i w table
plot $Data u 1:2 every ::c index i w table # add last value
plot '+' u ("") every ::::1 w table # add two empty lines
}
unset table
set key top left
plot for [i=0:2] $myStep u 1:2 index i w step lc i lw 3 notitle,\
for [i=0:2] $Data u 1:2 index i w p pt 7 lc i notitle, \
### end of code
Result:

How to display multiple symbols per line in gnuplot legend?

In gnuplot I have multiple datasets and wish to display on one line of the legend two different symbols. Here's what I currently have:
What I would like is to have to the square and the triangle to be on the same line. I am using the epslatex terminal. Thanks a lot.
You will have to do it by hand.
The most reliable way is probably to make all the key by hand :
Remove all automatic key by issuing unset key
Set custom variables to position your key:
KEY_X=0.1
KEY_Y=0.9
JUMP=0.05
IDX=1
For each plot command, issue before it a corresponding set label (point styles) or set arrow (line styles). E.g.,
set label IDX "a" at graph KEY_X,graph KEY_Y+(IDX-1)*JUMP left point pt 7 lc IDX offset 2
replot x with points pt 7 lc IDX
IDX=IDX+1
Thus you can add an empty label at the desired position when it comes to have two series with the same key.
Just in case this might still be of interest. Here are two ugly workarounds:
reduce the key spacing to 0.5 and plot two NaN dummies with ps 0 and set title "\n".
The symbols will not be (horizontally) next to each other but at least (vertically) close to each other.
set maxrow 3 and samplen 0 and plot one NaN dummy. Although the symbols are (horizontally) next to each other, currently, I don't have an idea how to get them closer to each other.
Code:
### "one" key entry with two symbols
reset session
set samples 20
set multiplot layout 1,2
set key top center spacing 0.5
plot x w p pt 7 ps 1 lc "red" title "a", \
NaN w p ps 0 title "\n", \
x*2 w p pt 5 ps 1 lc "blue" title "b", \
x*3 w p pt 9 ps 1 lc "black" title "\n", \
NaN w p ps 0 title "\n", \
x*4 w l dt 2 lw 1 lc "black" title "c"
set key top center maxrow 3 spacing 1 samplen 0
plot x w p pt 7 ps 1 lc "red" title "a", \
x*2 w p pt 5 ps 1 lc "blue" title "b", \
x*4 w l dt 2 lw 1 lc "black" title "c", \
NaN w p ps 0 title "\n", \
x*3 w p pt 9 ps 1 lc "black" title "\n"
unset multiplot
### end of code
Result:

Multiple y axis sections in a multiplot

I'v got a multiplot 2,2 with 5 lines in each plot, the lines range around the same values from 600-700 on the y value so they're overlapping.
I added +60 +120 +180 and +240 to the y values and it would do the trick but isn't there a way in gnuplot to add multiple sections on the y axes. So I'd plot the first line in the first section, second in the second and so on and each section ranges from 600-700
one of the 4 plots in the multiplot looks like this:
My Code:
set multiplot layout 2,2 title "Spring Summer Fall Winter"
set title 'Spring'
plot 'Data1.csv' every ::614::637 using 2:3 w l title 'CI 0.2',\
"" every ::963::964 using 2:($3+20) w l title 'CI 0.4 [{/Symbol l}ave+20]',\
"" every ::1080::1084 using 2:($3+40) w l title 'CI 0.6 [{/Symbol l}ave+40]',
set title 'Sommer'
plot 'Data2.csv' every ::1074::1155 using 2:3 w l title 'CI 0.2',\
"" every ::2548::2618 using 2:($3+60) w l title 'CI 0.4 [{/Symbol l}ave+60]',\
"" every ::4017::4092 using 2:($3+120) w l title 'CI 0.6 [{/Symbol l}ave+120]',\
"" every ::5580::5655 using 2:($3+180) w l title 'CI 0.8 [{/Symbol l}ave+180]',\
"" every ::7186::7280 using 2:($3+240) w l title 'CI 1.0 [{/Symbol l}ave+240]',
set title 'Fall'
plot 'Data3.csv' every ::43::43 using 2:3 w l title 'CI 0.2',\
"" every ::63::63 using 2:($3+20) w l title 'CI 0.4 [{/Symbol l}ave+20]',\
"" every ::87::87 using 2:($3+40) w l title 'CI 0.6 [{/Symbol l}ave+40]',\
"" every ::135::139 using 2:($3+60) w l title 'CI 0.8 [{/Symbol l}ave+60]',\
"" every ::311::362 using 2:($3+80) w l title 'CI 1.0 [{/Symbol l}ave+80]',
set title 'Winter'
plot 'Data4.csv' using 2:3 every ::788::829 w l title 'CI 0.2',\
"" every ::1256::1268 using 2:($3+20) w l title 'CI 0.4 [{/Symbol l}ave+20]',\
"" every ::1512::1525 using 2:($3+40) w l title 'CI 0.6 [{/Symbol l}ave+40]',\
"" every ::1681::1686 using 2:($3+60) w l title 'CI 0.8 [{/Symbol l}ave+60]',
unset multiplot
unset output
So what I've already got is a Multiplot with one plot for each season:
In every Plot there are up to 5 lines showing some data. Most of the data on the y axes is in an area between 600 and 700 so in order to avoid overlapping lines which are hard to distinguish even with different colors I plotted the first line normally, and then all the other lines with some additional point (+40 +60and so on) so that all the lines are not on the same spot in the plot but on top of each other, so you can compare tendencies and drifts.
What I want to know is if theres a way to simply cut the y axes into up to 5areas each reaching from 600-700so I can plot one line in the top area (600-700) the next one in the are below the one before and so on. So all lines are in the same value field but on top of each other so you can distinguish between them easier but and don't have to mind the +20 or whatever value when looking ad the y axes, since each of those areas on the y axes could have tics and a grid.
Here's an image I made ...
Proof of concept rather than a a complete solution but I think you can use this and start customizing from here:
set multiplot layout 6, 2
set format x "" # no xtic labels
set ytics -4, 2, 4 # adequately spaced, non-overlapping ytic labels
set bmargin 0
plot[][-5:5] "so.dat" every ::0::4 w lp t "spring 1"
plot[][-5:5] "so.dat" every ::5::9 w lp t "summer 1"
set tmargin 0
plot[][-5:5] "so.dat" every ::10::14 w lp t "spring 2"
plot[][-5:5] "so.dat" every ::0::4 w lp t "summer 2"
set bmargin 1
plot[][-5:5] "so.dat" every ::15::19 w lp t "spring 3"
plot[][-5:5] "so.dat" every ::10::14 w lp t "summer 3"
set tmargin 1
set bmargin 0
plot[][-5:5] "so.dat" every ::0::4 w lp t "fall 1"
plot[][-5:5] "so.dat" every ::5::9 w lp t "winter 1"
set tmargin 0
plot[][-5:5] "so.dat" every ::10::14 w lp t "fall 2"
plot[][-5:5] "so.dat" every ::20::24 w lp t "winter 2"
set bmargin 1
plot[][-5:5] "so.dat" every ::5::9 w lp t "fall 3"
plot[][-5:5] "so.dat" every ::10::14 w lp t "winter 3"
unset multiplot
Resulting graph:
Data file so.dat:
1 -1.8351
2 0.6315
3 -1.3365
4 2.1251
5 -0.6708
6 -3.3965
7 -0.2298
8 0.4807
9 -2.4213
10 -0.5998
11 -1.0238
12 -0.2025
13 0.4362
14 -1.1263
15 3.3197
16 0.0337
17 -0.7374
18 1.1504
19 -0.1656
20 -0.4389
21 1.4645
22 1.6538
23 1.6362
24 -2.0363
25 -4.9741

gnuplot - conditional colors in a points plot

I have the following data:
ClockIndex Max AvgStd Avg Num Threshold
"ck1 (1.54 GHz)" 35 +16.30 11 11583 X
"ck2 (1.54 GHz)" 28 +16.66 12 10669 -
"ck3 (1.54 GHz)" 29 +14.47 9 8036 -
"ck4 (1.54 GHz)" 35 +18.99 12 5685 -
"ck5 (1.54 GHz)" 9 +6.04 3 11 -
I'm plotting columns 2, 3, 4 together in a points plot, one line per entry.
Using this code:
set xtics rotate
set xlabel ""
set ylabel "Levels"
set title "Levels - foo"
set key autotitle columnhead
set term png medium size 1200,600
set grid
set output "foo.png"
plot "foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black
**Goal : If there's a "X" in the "Threshold" column, I want the "Max" point to be green, not blue.
I've tried using awk, but to no avail.
plot "< awk '{if($6 == \"X\") print }' foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black"
"< awk '{if($6 == \"-\") print }' foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "green",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black"
I keep getting errors like:
"foo_gnuplot", line 11: warning: Skipping data file with no valid points
Any help would be greatly appreciated! Thanks much for taking a look! :)
You can do it like this:
plot "foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black",\
"" using (strcol(6) eq "X" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "green"
(condition)?($plotThisValue):(1/0) is a common gnuplot technique for conditional plotting.
With that code the original point is overprinted with the green one. (If you want to remove the original (now hidden) blue point completely you can use the same technique in line one of your plot command.)
Thanks much to havogt for the great answer! Here's what I ended up doing:
plot "foo.rpt" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black",\
"" using (strcol(6) eq "X" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "green"
"" using (strcol(6) eq "-" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "blue"
Works great! Really appreciate the help!

Gnuplot: plotting points with variable point types

I have x,y values for points in the first 2 colums and a number that indicates the point type (symbol) in the 3. column, in one data file. How do I plot data points with different symbols?
Unfortunately, there isn't a way (AFAIK) to automatically set the point of the plot from a column value using vanilla GNUPLOT.
However, there is a way to get around that by setting a linestyle for each data series, and then plotting the values based on that defined style:
set style line 1 lc rgb 'red' pt 7 #Circle
set style line 2 lc rgb 'blue' pt 5 #Square
Remember that the number after pt is the point-type.
Then, all you have to do is plot (assuming that the data in "data.txt" is ordered ColX ColY Col3):
plot "data.txt" using 1:2 title 'Y Axis' with points ls 1, \
"data.txt" using 1:3 title 'Y Axis' with points ls 2
Try it here using this data (in the section titled "Data" - also note that column 3 "Symbol" is noted used, it's mainly there for illustrative purposes):
# This file is called force.dat
# Force-Deflection data for a beam and a bar
# Deflection Col-Force Symbol
0.000 0 5
0.001 104 5
0.002 202 7
0.003 298 7
And in the Plot Script Heading:
set key inside bottom right
set xlabel 'Deflection (m)'
set ylabel 'Force (kN)'
set title 'Some Data'
set style line 1 lc rgb 'red' pt 7
set style line 2 lc rgb 'blue' pt 5
plot "data.txt" using 1:2 title 'Col-Force' with points ls 1, \
"data.txt" using 1:3 title 'Beam-Force' with points ls 2
The one caveat is of course that you have have to reconfigure your data input source.
REFERENCES:
http://www.gnuplotting.org/plotting-single-points/
http://www.gnuplotting.org/plotting-data/
Here is a possible solution (which is a simple extrapolation from gnuplot conditional plotting with if), that works as long as you don't have tens of different symbols to handle.
Suppose I want to plot 2D points in a coordinate system. I have only two symbols, that I arbitrarily represented with a 0 and a 1 in the last column of my data file :
0 -0.29450470209121704 1.2279523611068726 1
1 -0.4006965458393097 1.0025811195373535 0
2 -0.7109975814819336 0.9022682905197144 1
3 -0.8540692329406738 1.0190201997756958 1
4 -0.5559651851654053 0.7677079439163208 0
5 -1.1831613779067993 1.5692367553710938 0
6 -0.24254602193832397 0.8055955171585083 0
7 -0.3412654995918274 0.6301406025886536 0
8 -0.25005266070365906 0.7788659334182739 1
9 -0.16853423416614532 0.09659398347139359 1
10 0.169997438788414 0.3473801910877228 0
11 -0.5252010226249695 -0.1398928463459015 0
12 -0.17566296458244324 0.09505800902843475 1
To achieve what I want, I just plot my file using conditionals. Using an undefined value like 1/0 results in no plotting of the given point:
# Set styles
REG_PTS = 'pointtype 7 pointsize 1.5 linecolor rgb "purple"'
NET_PTS = 'pointtype 4 pointsize 1.5 linecolor rgb "blue"'
set grid
# Plot each category with its own style
plot "data_file" u 2:($4 == 0 ? $3 : 1/0) title "regular" #REG_PTS, \
"data_file" u 2:($4 == 1 ? $3 : 1/0) title "network" #NET_PTS
Here is the result :
Hope this helps
Variable pointype (pt variable) was introduced (I guess) not until gnuplot 5.2.0 (Sept 2017) (check help points).
Just in retrospective, another (awkward) solution would be the following for those who are still using such early versions.
Data:
1 1.0 4 # empty square
2 2.0 5 # filled square
3 3.0 6 # empty circle
4 4.0 7 # filled circle
5 5.0 8 # empty triangle up
6 6.0 9 # filled triangle down
7 7.0 15 # filled pentagon (cross in gnuplot 4.6 to 5.0)
Script: (works from gnuplot>=4.6.0, March 2012; but not necessary since 5.2.0)
### variable pointtype for gnuplot>=4.6
reset
FILE = 'SO23707979.dat'
set key noautotitle
set offsets 1,1,1,1
set pointsize 4
stats FILE u 0 nooutput
N = STATS_records # get the number of rows
p0=x1=y1=NaN
plot for [n=0:N-1 ] FILE u (x0=x1, x1=$1, x0):(y0=y1, y1=$2, y0):(p0=$3) \
every ::n::n w p pt p0 lc rgb "red", \
FILE u 1:2 every ::N-1::N-1 w p pt p0 lc rgb "red"
### end of script
Result:

Resources