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:
Related
I have three different lines, where colours and their dashes means different things which I want on one plot, instead of three. How do I accomplish that?
set datafile separator comma
$sample <<EOD
2020-01-01,4,UK,Business
2020-02-01,4,UK,Business
2020-01-01,4,UK,Social
2020-02-01,15,UK,Social
2020-01-01,1,USA,Social
2020-02-01,25,USA,Social
EOD
set format x '%Y'
set xdata time
set timefmt "%Y-%m-%d"
plot '$sample' u 1:2 title "UK/Business" with linespoints dt 3 lw 5 pt 7 lc "red"
plot '$sample' u 1:2 title "USA/Social" with linespoints dt 3 lw 1 pt 7 lc "blue"
plot '$sample' u 1:2 title "UK/Social" with linespoints dt 3 lw 5 pt 7 lc "blue"
E.g. blue is "Social" and lw 1 (fine dots) is for USA.
Please check the manual or help plot. Plot elements for the same plot are separated by comma.
Syntax:
plot {<ranges>} <plot-element> {, <plot-element>, <plot-element>}
To improve readability you can split long code lines into several lines by using \. Note that no character is allowed after \ in the same line except carriage return/line feed.
Try this:
plot '$sample' u 1:2 title "UK/Business" w lp dt 3 lw 5 pt 7 lc "red", \
'' u 1:2 title "USA/Social" w lp dt 3 lw 1 pt 7 lc "blue", \
'' u 1:2 title "UK/Social" w lp dt 3 lw 5 pt 7 lc "blue"
Addition: (a filter depending on two (string)-columns)
You can implement a filter by using the ternary operator. Check help ternary and help strcol. A value which does not pass the filter will be set to NaN. If you still want to your points being connected with lines, you need to set datafile missing NaN.
Code:
### filtered data with different line colors
reset session
set datafile separator comma
$sample <<EOD
2020-01-01,4,UK,Business
2020-02-01,4,UK,Business
2020-01-01,4,UK,Social
2020-02-01,15,UK,Social
2020-01-01,1,USA,Social
2020-02-01,25,USA,Social
EOD
myTimeFmt = "%Y-%m-%d"
set format x '%d.%m.%Y' timedate
set key top left
set datafile missing NaN
myFilter(colData,colFilter1,key1,colFilter2,key2) = (strcol(colFilter1) eq key1) && (strcol(colFilter2) eq key2) ? column(colData) : NaN
plot $sample u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"UK",4,"Business")) w lp dt 3 lw 5 pt 7 lc "red" title "UK/Business", \
'' u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"USA",4,"Social")) w lp dt 3 lw 1 pt 7 lc "blue" title "USA/Social" , \
'' u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"UK",4,"Social")) w lp dt 3 lw 5 pt 7 lc "blue" title "UK/Social"
### end of code
or a bit shortened if the columns 2,3,4 do not change within the plot command...
...
...
myTime(col) = timecolumn(col,myTimeFmt)
myFilter(key1,key2) = (strcol(3) eq key1) && (strcol(4) eq key2) ? column(2) : NaN
plot $sample u (myTime(1)):(myFilter("UK","Business")) w lp dt 3 lw 5 pt 7 lc "red" title "UK/Business", \
'' u (myTime(1)):(myFilter("USA","Social")) w lp dt 3 lw 1 pt 7 lc "blue" title "USA/Social" , \
'' u (myTime(1)):(myFilter("UK","Social")) w lp dt 3 lw 5 pt 7 lc "blue" title "UK/Social"
Result:
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:
I basically want this (first diagram) done with gnuplot. I've searched and found nothing exactly like this. I can do a good heaviside without the little circles at the end and start of the two lines, but I can't seem to get it with the little circles. Actually, the second diagram would be nice to know too. The third too, but I'm not greedy.
Just for the records and completeness... although you can define a function
H(x) = x<0 ? 0 : 1
If you plot
plot H(x) w l
the line will be continuous at zero and of course without points.
So, another suggestion with just two columns x,y and variable pointtype would be the following.
Code:
### Heaviside function
reset session
$Heaviside <<EOD
-2 0
0 0
0 0.5
0 1
2 1
EOD
set yrange [-1:2]
set ytics 1
unset key
set multiplot layout 3,1
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:($0==1||$0==3?$2:NaN):($0==3?7:6) w p pt var lc 0
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:($0==1||$0==2||$0==3?$2:NaN):($0==2?7:6) w p pt var lc 0
set xrange [0:4]
a = 3
plot $Heaviside u ($1+a):2 w l lc 0, \
'' u ($1+a):($0==1||$0==3?$2:NaN):($0==3?7:6) w p pt var lc 0
unset multiplot
### end of code
Result:
Addition:
A variation with shorter and less confusing plot command, but using 4 columns together with variable pointtype. This will give the same result as above.
Code:
### Heaviside function
reset session
$Heaviside <<EOD
-2 0 NaN NaN
0 0 6 6
0 0.5 NaN 7
0 1 7 6
2 1 NaN NaN
EOD
set yrange [-1:2]
set ytics 1
unset key
set multiplot layout 3,1
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:2:3 w p pt var lc 0
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:2:4 w p pt var lc 0
set xrange [0:4]
a = 3
plot $Heaviside u ($1+a):2 w l lc 0, \
'' u ($1+a):2:3 w p pt var lc 0
unset multiplot
### end of code
Addition 2:
In order to finalize the answer, here is an approach to plot functions containing the Heaviside function.
Instead of plotting from a datablock with fixed x-values (as in the two examples above) it uses the current x-range. Note, for example the syntax plot '+' u 1:(sin($1)) is basically identical with plot sin(x).
Apparently, setting the line color via lc rgb -1 does not plot a line, which can be used here to interrupt the line. You may want to increase the samples, e.g. set samples 300 to avoid a gap between the points and the function.
Code:
### plotting Heaviside function and functions containing Heaviside function
# including line interruption and inclusion/exclusion points
reset session
Heaviside(x,a) = x<a ? 0 : 1 # definition of Heaviside function
array Hpoints[2] = [6,7] # array for plotting "Heaviside points"
Hcolor(x) = (x0=x1, x1=x, x0<a && x1>=a ? -1 : 0xff0000) # set color -1 for line interruption
dx(n) = 1e-3*(2*n-1) # small dx to get y-value of points close to break
f(x,a) = 50/(x**2+2)*cos(4*x) * Heaviside(x,a)
unset key
set multiplot layout 2,1
a = 2.0
set yrange[-1:2]
plot x1=NaN '+' u 1:(Heaviside(x,a)):(Hcolor(x)) w l lc rgb var, \
Hpoints u (a):(Heaviside($1,a)):(Hpoints[$0+1]) w p pt var lc rgb Hcolor(NaN)
a= 0
set samples 300
set yrange[-25:35]
plot x1=NaN '+' u 1:(f(x,a)):(Hcolor(x)) w l lc rgb var, \
Hpoints u (a):(f(a+dx($0),a)):(Hpoints[$0+1]) w p pt var lc rgb Hcolor(NaN)
unset multiplot
### end of code
Result:
I created the following datafile (mind the two empty lines):
-2 0 0 1
0 0 2 1
0 0 0 1
and ran the following gnuplot commands:
set yrange [-2:2]
plot "file" using 1:2 with lines,\
"" using 3:4 with lines, \
"" index 1 using 1:2 with points pointtype 6, \
"" index 1 using 3:4 with points pointtype 7
Fix the colours to your liking.
I have a plot with several types of objects (each read from a separate file). I'm plotting the same several functions for all of them, all on the same graph (same X-axis).
I set the markers (pt) explicitly for each, and the color (lc), so the same object has the same marker, but the same function has the same color. As an example we have 2 files, one for each object (| is just to separate the files here):
0 0 0 | 0 1 1
1 1 2 | 1 1 2
Let's call the left file A, the right B. Column 1 in each file is the x axis, column 2 is using 1:2, and column 3 is using 1:3. So using the above files in an interactive session:
gnuplot> plot "A" using 1:2 with lp pt 1 lc 'black'
gnuplot> replot "A" using 1:3 with lp pt 1 lc 'red'
gnuplot> replot "B" using 1:2 with lp pt 2 lc 'black'
gnuplot> replot "B" using 1:3 with lp pt 2 lc 'red'
we get:
Is it possible to have the key separated, so A/B appear next to their respective marker, and the function name ("using...") appears next to a line (or anything) with the appropriate color?
Right now by omitting titles (notitle in the plot command) I can get one or the other, though I would have to settle on some uniform arbitrary marker/color (depending on what I chose to set as key). Can I:
Get two keys somehow? - Preferably setting the missing attribute (color or marker) to something not in the plot.
If not, can I customize a manual legend somehow?
I am not fully sure what you want to achieve, nevertheless as for the splitting of the key, I don't think that Gnuplot has some "out-of-the-box" feature for this. However, you could (ab)use multiplot to achieve this effect. The idea is basically to generate two overlapping plots - one with points and one with lines - and to position the keys independently:
set terminal pngcairo rounded font ",16"
set output 'fig.png'
$A << EOD
0 0 0
1 1 2
EOD
$B << EOD
0 1 1
1 1 2
EOD
set multiplot
set xtics out nomirror
set ytics out nomirror
eps = 0.1
set lmargin at screen eps
set rmargin at screen 1 - eps/2
set bmargin at screen eps
set tmargin at screen 1 - eps/2
#common key settings
set key left top Left reverse spacing 1.5
set key at screen 0.1,screen 1-eps
plot \
$A u 1:2 with p ps 1.5 pt 1 lc 'black' t 'A', \
$A u 1:3 with p ps 1.5 pt 1 lc 'red' t 'A' , \
$B u 1:2 with p ps 1.5 pt 2 lc 'black' t 'B', \
$B u 1:3 with p ps 1.5 pt 2 lc 'red' t 'B'
unset border; unset xtics; unset ytics
set key at screen 0.3,screen 1-eps
plot \
$A u 1:2 with l lc 'black' t 'using 1:2', \
$A u 1:3 with l lc 'red' t 'using 1:3', \
$B u 1:2 with l lc 'black' t '', \
$B u 1:3 with l lc 'red' t ''
This would give you:
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
}