Question

I'm plotting some data curves with gnuplot, and they look like this:

Plot with thin lines

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:

Plot with thick lines

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?

Was it helpful?

Solution

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.

OTHER TIPS

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' 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top