Same Gnuplot linestyle as in graph in key/legend - gnuplot

I would like to plot a legend/key, which shows the different symbols on the line. Currently my plots look like this:
Unfortunately the symbols (triangle, rectangle and circle) are not shown in the key/legend. How is it possible to add them?
I use the following gnuplot script:
set title tit font "palatino,20"
set xlabel xlbl font "palatino,20"
set ylabel ylbl font "palatino,20"
#set logscale x
set output graphfilename.".pdf"
set terminal pdf
set border linewidth 2
set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 5 # --- blue
set style line 2 lc rgb '#00ad60' lt 1 lw 2 pt 7 # red .
set style line 3 lc rgb '#ad0000' lt 1 lw 2 pt 9 # green .
set tics scale 0.8
set key below
plot file1 using ($1/1000):($2/1000000):($3/1000000):($4/1000000) notitle w yerrorbars ls 1, \
'' using ($1/1000):($2/1000000) title "Hlog" w lines ls 1,\
file2 using ($1/1000):($2/1000000):($3/1000000):($4/1000000) notitle w yerrorbars ls 2, \
'' using ($1/1000):($2/1000000) title "Iris" w lines ls 2,\
file3 using ($1/1000):($2/1000000):($3/1000000):($4/1000000) notitle w yerrorbars ls 3, \
'' using ($1/1000):($2/1000000) title "Java" w lines ls 3

Generally, you can get both lines and points, if you plot with the linespoints plotting style:
sc(x) = x*1e-6
plot file1 using ($1/1000):(sc($2)):(sc($3)):(sc($4)) notitle w yerrorbars ls 1 ps 0.5, \
'' using ($1/1000):(sc($2)) title "Hlog" w linespoints ls 1
That draws the points twice, which shouldn't be a problem unless you use transparency. I also shrinked the points which are drawn together with the errorbars to 50%, so you don't get problems with antialiasing.
As another option you could add the title only the the errorbars, in which case the legend would look like |---x---| (i.e. contain also the errorbars):
sc(x) = x*1e-6
plot file1 using ($1/1000):(sc($2)):(sc($3)):(sc($4)) title "Hlog" w yerrorbars ls 1, \
'' using ($1/1000):(sc($2)) notitle w lines ls 1

Related

Cannot plot straight lines in gnuplot without showing different point types

I'm using gnuplot to plot a figure, but the plot keeps giving me different point types instead of a straight line. I want to plot them using only straight lines but keep getting x's or plus signs or different symbols. Here is what I have for the gnuplot script.
set terminal pdf
set output "temperatures.pdf"
set style line 1 lc rgb "red" lt 1
set style line 2 lc rgb "blue" lt 1
set style line 3 lc rgb "purple" lt 1
set style line 4 lc rgb "orange" lt 1
set style line 5 lc rgb "cyan" lt 1
set xrange [0:780]
set yrange [0:88]
set xlabel "Time (s)"
set ylabel "Temperature (°C)"
set key bottom right
plot "data.dat" using 6:1 ls 1 notitle, "data.dat" using 6:2 ls 2 notitle, "data.dat" using 6:3 ls 3 notitle, "data.dat" using 6:4 ls 4 notitle, "data.dat" using 6:5 ls 5 notitle, \
NaN ls 1 title "600 MHz", NaN ls 2 title "800 MHz", NaN ls 3 title "1100 MHz", NaN ls 4 title "1300 MHz", NaN ls 5 title "1500 MHz"
There are different plotting styles, e.g. with points, with lines, with linespoints and many others. You can also abbreviate the the styles via w p, w l and w lp. Check help with.
If you don't specify anything, the default is with points. That's what you are getting. Setting a line style or linetype does not necessarily mean that you are plotting only a line. You also have to explicitely use with lines.
By the way, you can use the last used file by specifying ''. And for readability you can write multiple lines by separating with \ (note, \ must be the last character in the line, no space or others characters allowed afterwards).
Try the following:
plot "data.dat" u 6:1 w l ls 1 title "600 MHz", \
'' u 6:2 w l ls 2 title "800 MHz", \
'' u 6:3 w l ls 3 title "1100 MHz", \
'' u 6:4 w l ls 4 title "1300 MHz", \
'' u 6:5 w l ls 5 title "1500 MHz"

Separate key (legend) for colors and markers

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:

Gnuplot histogram with plot line

I am using Gnuplot to create a histogram with a plot line, however, the plot line is not fit well with bar head, also I would like to put the line a little bit far from the bar head.
set border 3
set boxwidth 0.9
set tics nomirror out scale 0.75
set style fill solid 0.8
plot "03.txt" using 2:xtic(1) lt rgb "#0060ad" notitle, "" using 2 smooth csplines notitle with lines ls 1, "" using 3 lt rgb "#ff6600" notitle, "" using 3 smooth csplines notitle with lines ls 2, "" using 4 lt rgb "#dd181f" notitle, "" using 4 smooth csplines notitle with lines ls 3
Updated:
This is the data file:
500000 25.938 25.938 2
1000000 52.385 52.385 4
1500000 79.749 78.405 6.125
2000000 152.589 100.261 12.479
2500000 224.869 118.364 19.159
This should work for any number of columns, you have to specify them in the variable N, and number them in calls to custom function xbox. This should do for a non-intensive usage. You can offset vertically the curves with the OFFSET variable (in units of y axis)
set border 3
#number of columns to be plotted
N=3
#vertical offset
OFFSET=0
#gapwidth (set to gnuplot's default)
GW=2
xbox(x,i)=x+(i-N*0.5)/(N+GW)
set boxwidth 0.9
set tics nomirror out scale 0.75
set style fill solid 0.8
plot "03.txt" using 2:xtic(1) lt rgb "0060ad" notitle, \
"" using 2 with histogram notitle, \
"" using (xbox($0,1)):($2+OFFSET) smooth csplines notitle with lines ls 1, \
"" using 3 lt rgb "#ff6600" notitle with histogram, \
"" using (xbox($0,2)):($3+OFFSET) smooth csplines notitle with lines ls 2, \
"" using 4 lt rgb "#dd181f" notitle with histogram, \
"" using (xbox($0,3)):($4+OFFSET) smooth csplines notitle with lines ls 3

How to make key colour black in Gnuplot

How can I make the key "symbols" black in colour when using palettes?
I think you cannot control this directly, here a workaround:
plot 'MOD1.dat' u 2:3:1 w p pt 7 ps 2 lt black, \
'MOD1.dat' u 2:3:1 w p pt 7 ps 2 lt palette notitle
So, we first plot the data without legend in black, then plot the data points but no legend. The nice thing about this approach is that there is no need to fix the x or y range.
I would just create a dummy line and use notitle on the real lines. Something like
set yrange[0:1]
plot "realdata1.data" u 1:2 w linespoints lt 1 notitle , \
"realdata2.data" u 1:2 w linespoints lt 2 notitle , \
1/0 w linespoints lt 1 lc rgb 'black' title 'Model1', \
1/0 w linespoints lt 1 lc rgb 'black' title 'Model2'
Then replace lt 1 with whatever you have used for style in your current plot. Note that you'll need to use set yrange for gnuplot to accept the dummy curve

Thicker lines in the legend of gnuplot

I'm plotting some data curves with gnuplot, and they look like this:
However, the line samples in the legend are too thin. When you have more curves, it becomes hard to distinguish the colors. You can increase the thickness of the curves using "linewidth", e.g., by adding "lw 3" to the plot command, and you'd get this:
However, this increases the thickness everywhere. Is it possible to make the lines thick in the legend only? I know it can be done "the other way", by postprocessing on the output .png file. But is there a direct approach, using some gnuplot setting/wizardry?
Unfortunately, I don't know a way to control the thickness of the lines in the key, since they correspond to the lines being drawn. You can see what you can change by typing help set key in gnuplot.
Using multiplot, you can draw the plot lines first without the key, then draw the key again for 'ghost lines'. Here is a code sample which would do that:
set terminal png color size 800,600
set output 'plot.png'
set multiplot
unset key
plot '../batteries/9v/carrefour.txt' w lp, \
'../batteries/9v/philips.txt' w lp, \
'../batteries/9v/sony.txt' w lp
set key; unset tics; unset border; unset xlabel; unset ylabel
plot [][0:1] 2 title 'Carrefour' lw 4, \
2 title 'Philips' lw 4, \
2 title 'Sony' lw 4
In the second plot command, the function 2 (a constant) is being plotted with a y range of 0 to 1, so it doesn't show up.
I ran across this post and it gave me a critical idea.
The provided solution does not work in multiplot mode, since the second plot command will trigger the second plot, which is most likely not desired.
as a workaround one can set the original data as "notitle", then plot data outside of range with the same linetype and color in different thickness with the desired title. I'll just leave my current example here. It also includes linestyles that i have declared. So i just use the same linestyle (ls) to get the same color but change the thickness on the second line.
# for pngs
set terminal pngcairo size 1600,600 font ',18' enhanced
set output "pic_multi_kenngr_ana.png
set style line 2 lc rgb '#0ce90b' lt 1 lw 1.5 # --- green
set style line 3 lc rgb '#09e0b3' lt 1 lw 1.5 # .
set style line 4 lc rgb '#065fd8' lt 1 lw 1.5 # .
set style line 5 lc rgb '#4e04cf' lt 1 lw 1.5 # .
set style line 6 lc rgb '#c702a9' lt 1 lw 1.5 # .
set style line 7 lc rgb '#bf000a' lt 1 lw 1.5 # --- red
set multiplot layout 1,2
set xtics rotate
set tmargin 5
set xtics 12
set grid xtics
# set axis labels
set ylabel 'T [K]'
set xlabel 'Zeit [h]'
# select range
set xrange [0:48]
set yrange [290.15:306.15]
set title "(a) Bodentemperatur"
set key top right Right
plot 'par_crank_hom01lvls.04.dat' u 1:3 with lines ls 7 notitle,\
'par_crank_str01lvls.16.dat' u 1:3 with lines ls 2 notitle,\
500 t 'z = 4 cm' ls 7 lw 4,\
500 t 'z = 16 cm' ls 2 lw 4
################################################
set title "(b) Bodenwärmestrom an der Oberfläche"
set ylabel 'G [W m^{-2}]'
set yrange[-110:110]
unset key
plot 'par_crank_str01_ghf.dat' u 1:3 with lines
unset multiplot
I hope this will help someone
An even more simple work-around (imho) is to define the colours explicitly and plot each line twice, once with high lw for the key and also with the title to appear in the key, but adding "every ::0::0" which effectively ends up in plotting nothing, and once the normal way. See the following code snippet:
plot data u 0:1 w l linecolor rgb #1b9e77 lw 2 t "",\
data every ::0::0 u 0:1 w l linecolor rgb #1b9e77 lw 4 t "Title"
To expand on the NaN comment by #Svalorzen, the following will graph two lines of width 1 from some datafile.txt with no titles and create matching blank lines with the specified titles and width 5 for the key only:
plot [][]\
NaN title "Title1" w line lt 1 lc 1 lw 5,\
NaN title "Title2" w line lt 1 lc 2 lw 5,\
"datafile.txt" using 1:2 title "" w line lt 1 lc 1 lw 1,\
"datafile.txt" using 1:3 title "" w line lt 1 lc 2 lw 1
I find an answer for this:
Set key linewidth
in your case, must be:
plot '../batteries/9v/carrefour.txt' w l lw 1 linetype 1 notitle, 0/0 linetype 1 linewidth 5 title 'Carrefour'
rep '../batteries/9v/philips.txt' w l lw 1 linetype 2 notitle, 0/0 linetype 2 linewidth 5 title 'Philips'
rep '../batteries/9v/sony.txt' w l lw 1, linetype 3 notitle, 0/0 linetype 3 linewidth 5 title 'Sony'
Try something like:
plot # ... \
keyentry w l lw 1 lc 2 t "Title" # ...
And remove the old keys.

Resources