Gnuplotting multi-plot data without a textfile - gnuplot

I've just read Gnuplotting data without a textfile, and I want to do the same thing, but with a "multi-plot". I currently have:
plot 'data.csv' using 1:3:2:6:5:7:xticlabels(8) with candlesticks title 'Quartiles' whiskerbars, \
'' using 1:4:4:4:4:4 with candlesticks lt -1 notitle
and I want to inline the data in data.csv.

This is easy enough:
set multiplot layout 1,2
plot '-' u 1:2
1 2
2 3
3 4
e
plot '-' u 1:2
2 3
3 4
4 5
e
Note that inline data is not really particularly happy with the '' pseudofile. You would actually need to include your entire data again at that point. So, If you want 2 traces on the same subplot of a multiplot:
set multiplot layout 1,2
plot '-' u 1:2, '-' u 1:3
1 2 3
4 5 6
7 8 9
e
1 2 3
4 5 6
7 8 9
e
plot '-' u 1:($2*$3)
1 2 3
4 5 6
7 8 9
e
This ends up being the same thing as if you had a datafile data.txt:
#data.txt
1 2 3
4 5 6
7 8 9
and plotted it with this (much simpler) script:
set multiplot layout 1,2
plot 'data.txt' u 1:2, '' u 1:3
plot '' u 1:($2*$3)

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:

categorising points by using of gnuplot

I am going to plot a file containing 4 columns. first and second column are x and y respectively. I want to categorize these point based on the third and fourth column. In fact, the third column should display the color (red or blue) of point and the fourth column should determine its type (square or circle ). how could I reach to this goal by gnu-plot?
I should mention that I tried this, by it does not work!
set style line 1 lc rgb 'red' pt 7
set style line 2 lc rgb 'red' pt 7
set style line 3 lc rgb 'blue' pt 9
set style line 4 lc rgb 'blue' pt 9
plot 'data' w ($3= 1 && $4= 1) ? p ls 1 \
: ($3= 1 && $4= 2) ? p ls 2 \
: ($3= 2 && $4= 1) ? p ls 3 \
: ($3= 2 && $4= 2) ? p ls 4
Please check help points. From the manual:
plot DATA using x:y:pointsize:pointtype:color \
with points lc variable pt variable ps variable
You don't show sample data. If you can adjust your data, the easiest would be the following below.
If you want to set the color by name in your file, check this.
Code:
### variable pointtype and color
reset session
$Data <<EOD
1 2 5 0xff0000
3 4 7 0xff0000
5 6 5 0x0000ff
7 8 7 0x0000ff
EOD
plot $Data u 1:2:3:4 w p ps 5 pt var lc rgb var
### end of code
Result:

How does gnuplot skip irrelevent column in plot?

I have a data file like this
# Time A irrelevent_col B
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
I am trying to plot two lines Time vs A, Time vs B with labels "A" and "B". How can I skip the "irrelevent_col" column?
I did the following, but the code still plots the "irrelevent_col" column. Shouldn't the ? : operator gets ride of that column?
set datafile commentschars "!!!"
plot for [i=2:4] filename using 1:(columnhead(i+1) ne "irrelevent_col" ? column(i) : 1/0) title columnhead(i+1)
Thanks!
If I understood correctly your question:
plot "filename" using 1:2 title "A" with lines,\
"filename" using 1:4 title "B" with lines
Let me repeat what I've understood from your question:
You have a large number of columns and you want to plot them all in a loop, but exclude a single column (or a few) by name.
Of course, you can specify all columns you do want to plot, like in #ViniciusPlacco's answer, however, as I understand that's what you wanted to avoid, since you have many more columns in your real data. You can also always use external tools to pre-process your data, but here I would like to suggest a gnuplot-only and hence platform-independent solution.
Why your solution is not working, I can only speculate: I guess using columnheader twice in a plot iteration creates problems (at least for gnuplot<=5.2). But I could be wrong. But as I will show below, your solution will work for gnuplot>=5.4.0.
Furthermore, you want to specify the columns by header not by column number.
In addition, your header line starts with the comment char '#', but you can easily change that to access the columnheader information.
In the example below you can specify a list of several headers which you don't want to plot. Maybe the script(s) can be further simplified.
Script: (works for gnuplot>=5.4.0, July 2020)
### exclude some columns by header from plotting loop (gnuplot>=5.4.0)
reset session
$Data <<EOD
# Time A B C D E
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
EOD
set datafile commentschars '' # no commentchar
set key top left noenhanced noautotitle
inList(w,list) = int(sum[_i=1:words(list)] w eq word(list,_i))
doNotPlot = 'B C'
color = 1
plot for [col=2:6] $Data u 1:((b=inList(myHeader=columnhead(col+1),doNotPlot)) ? \
NaN : ($0==1?color=color+1:0, column(col))) w lp pt 7 lc color ti (b ? '' : myHeader)
### end of script
Result:
For older gnuplot versions <5.4.0 you need a different approach:
get all headers into a string
specify all your headers of the columns you don't want to plot in a string
for gnuplot>=5.0.0, subtract two lists and keep the column numbers for the header you do want to plot
Script: (works for gnuplot>=5.2.2, Nov. 2017; result same as graph above)
### exclude some columns by header from plotting loop (gnuplot>=5.2.2)
reset session
$Data <<EOD
# Time A B C D E
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
EOD
set datafile commentschars '' # no commentchar
set key top left noenhanced noautotitle
inList(w,list) = int(sum[_i=1:words(list)] w eq word(list,_i))
doNotPlot = 'B C'
myHeaders = ''
color = 1
plot for [col=2:6] $Data u 1:((b=inList(myHeader=columnhead(col+1),doNotPlot)) ? NaN : \
($0==1 ? (color=color+1, myHeaders=myHeaders.' '.myHeader) : 0, column(col))) w lp pt 7 lc color, \
for [i=1:color] NaN w lp pt 7 lc i ti word(myHeaders,i)
### end of script
Script: (works for gnuplot>=5.0.0, Jan. 2015; result same as graph above)
### exclude some columns by header from plotting loop (gnuplot>=5.0.0)
reset session
$Data <<EOD
# Time A B C D E
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
EOD
set datafile commentschars '' # no commentchar
set datafile separator "\n" # or another character which is not in the header line
stats $Data u (allHeaders = strcol(1)[2:]) ever ::::0 nooutput # get header line into string
set datafile commentschar # reset to default
set datafile separator whitespace # ditto
inList(w,list) = int(sum[_i=1:words(list)] w eq word(list,_i))
subtractLists(list1,list2) = (_s=' ', sum[_j=1:words(list1)] (_s0=word(list1,_j), \
inList(_s0,list2) ? 0 : (_s=_s._s0.' ', \
myColNos=myColNos.' '._j), 0), _s)
doNotPlot = 'B C'
myColNos = ''
myHeaders = subtractLists(allHeaders,doNotPlot)
myColNo(i) = column((word(myColNos,i)))
set key top left noenhanced noautotitle
plot for [i=2:words(myHeaders)] $Data u 1:(myColNo(i)) w lp pt 7 ti word(myHeaders,i)
### end of script

Gnuplot 1d plot

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
}

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