Search code examples
plotgnuplotlegend

How to append user defined legend to existing legend in gnuplot


I create with gnuplot the graph below, but now I want to add to legend (titles) extra value that not inside plot data.

I draw vertical with set arrow and add label to identification.

set label "Ef" at (Ef+0.5),-20
#some other code
plot

and

set style line 11 lc rgb 'black' lt 1 dt "-" lw 1 pt 5 ps 1.5 pi 4  # --- black dashed
set style arrow 1 nohead front  ls 11
set arrow arrowstyle 1 from Ef,GPVAL_DATA_Y_MIN to Ef,GPVAL_DATA_Y_MAX
replot

Ef is a dummy variable (Ef = 5 in this case)

But I want add Ef to legend with dashed line.

How can I do this?


Solution

  • Gnuplot draws the whole plot at once, so there is no way to add something to an existing plot. Thus we have to "trick" it into drawing the legend entry for that line at the beginning.

    The best way is to plot a function which is undefined. When gnuplot encounters undefined points, it just skips them, so plotting the function 1/0 will cause no points to be drawn.

    Consider

    set arrow 1 from 2,graph 0 to 2,graph 1 lt 0 nohead
    plot [-5:5][-2:2] sin(x) t "Sine", 1/0 t "Extra"
    

    Example Image

    This will add a legend entry for that dotted line, but the plot command does not draw such a line.