bring legend to the front in gnuplot - gnuplot

Is there a way to bring the key (legend) in gnuplot (epslatex terminal) to the very front? In my plot I experience that some filledcurves are on top of the small line samples of the key so that these are invisible. The lables in the key are placed on top as they are drawn by latex. I know that I could change the order in the plot (filledcurves first) but I actually want that the filledcurves hide the previously drawn lines in the plot itself.

Recent gnuplot versions allow you to say "set key opaque", which I think does what you want.

First plot your lines without the key plot x notitle ls 1, then plot your filledcurves, then plot yet the extra lines using the linestyles of the first lines, but so that they are out of the visible area:
set yrange [-10:10]
plot -x notitle ls 2, x**2/3-5 w filledc ls 5, x**2+100 t "first-line legend" ls 2
This gives you the legend at the top of the filledcurves, but the visible lines below. Hope this works with epslatex too.

#sfeam's answer is correct, as long as the key doesn't have overlap with the border. If it has, there's no way to bring the legend in front of the border, but you can bring the border to the back by "set border back".
So, a combination of "set key opaque" and "set border back" guarantees that the legend is on top of everything.

One can use dummy plot-elements with NaN (or keyentry for gnuplot 5.2.6). The key title will not be in front.
Here is a comparison between default, the NaN approach and key opaque.
reset session
set samp 10000
a = 10
set xrange [0:200]
set multiplot layout 2,2
set key title "default"
plot sin(x), cos(x)
set key title "NaNs dummy entries"
plot sin(x) t "", cos(x) t "",\
NaN t "sin(x)" ls 1, NaN t "cos(x)" ls 2
set key opaque title "key opaque"
plot sin(x), cos(x)
unset multiplot

Related

Can I plot 1D heatmap with gnuplot?

I'm trying to plot a 1D heatmap using two columns of data (x value and y value) in gnuplot. The linegraph plotted using my data is like this:
Linegraph:
However after some trying I can only achieve this:
What I've got:
And what I want to get is something like this. (Only example)
What I want:
The gnuplot script that I use is as follows:
set view map
set size ratio 0.2
unset ytics
unset key
splot 'test.dat' u 1:(1):2 palette
Could anyone help please?
So you want to use the y axis as a fake dimension in order to increase the width of your second line plot?
Sure, this is e.g. possible with boxxyerror with explicit ymin and ymax errors that fill the yrange.
set xr [-10:10]
set yr [0:1]
xspacing = 0.1
plot '+' u 1:(0.5):($1-xspacing):($1+xspacing):(0):(1):(sin($1)) w boxxyerror lc palette
In your case replace the sin(x) with the respective column of your data. With the special file '+' the x-width has no effect, but in your case you might need to play around with a proper xspacing in order to avoid white gaps between the points.
I would do it like this:
unset key
set xrange noextend
set offset 0,0,graph .05,graph .05
set palette cubehelix negative
plot 'foo.dat' using 0:3 with lines lc "black", \
'foo.dat' using 0:(70):3 with lines lc palette lw 10

Gnuplot key box

I am trying to move the box of this key (legend) in gnuplot.
It looks like this right now but I want the key to be centered in the box.
If I make the box smaller the key makes a part of the box to disappear as well as can be seen in this picture
.
The code used the produce the first plot below is:
set xlabel 'Time'
set ylabel 'Rad/s'
set title 'Top 1'
set key box width 2 height 3 opaque
plot 'top1lspin.txt' using 1:5 with lines title '{/Symbol ~y{1.1.}}'
The same affect can be seen with other plot commands, e.g.:
set xlabel 'Time'
set ylabel 'Rad/s'
set title 'Top 1'
set key box width 2 height 3 opaque
plot sin(x) title '{/Symbol ~y{1.1.}}'
Without using some LaTeX-based terminal which would offer much more control than just the limited set of enhanced postscript commands, I am afraid that there are just "ugly" solutions to the problem.
One might:
dispense with the border on set key, generate the legend as is and then draw a "fake" border using set rectangle. However, the placement of the box here is rather annoying...
trick Gnuplot into using a "proper" alignment inside the legend as in the example below. The idea is to prepend an auxiliary character with under-printed character which will offset the over-printed dot over the "real" character \psi. This will provide more-or-less fine alignment with respect to the line sample in the legend. Alignment with respect to the box is then achieved by appending a phantom space.
In total:
set terminal postscript enhanced color
set output 'test.ps'
set xlabel 'x'
set ylabel 'y'
set title 'Top 1'
set key box vertical width 2 height 1 maxcols 1 spacing 3
set xr [0:pi]
set xtics nomirror
set ytics nomirror
plot \
sin(x) w l lw 3 lc rgb 'red' t '{/Symbol y}', \
cos(x) w l lw 3 lc rgb 'blue' t '#{/Symbol ~&{y}{-1.0&{.}}}{/Symbol ~y{0.8.}}#{/Symbol &{y}}'
this then produces:
Having an overprint as the last character seems to confuse the box drawing.
Try appending a space or "&1" or similar to the title; this seems to fix it. (But I had luck with '{/Symbol ~y{.3.}}&1'; not sure what your version is trying to do.)

Set title for arrow in gnuplot

I've used the arrow to draw a vertical line and I would like to title it so it's shown in the key. Is there a way to do it? As far as I can tell for the manual, there's no title option in the syntaxis for arrow, but I'm sure there's a workaround.
The only thing I think of is drawing the arrow with the same color as something outside the plot range and use its title, but it's rather clumsy.
I'm using the terminal pngcairo, just in case it's relevant.
You can plot something with vectors, which will give a title in the key. It plots arrows based on data points. The using statement is x:y:Δx:Δy where the tail is positioned at (x, y) and the head is at (x+Δx, y+Δy). For a vertical line, you can turn off the arrow head and use Δx of zero:
set terminal pngcairo dashed
set output 'plot.png'
set angles degrees
set xrange [0:360]
set yrange [-2:2]
plot sin(x), '-' using 1:(-2):(0):(4) with vectors nohead lc rgb 'black' title '90 degrees'
90
e
Gnuplot will ignore anything with an invalid value (1/0 for instance). You can take advantage of this to plot what you want.
Suppose that we set a vertical line with
set arrow from 1,graph 0 to 1,graph 1 nohead lt 0
Now, if I want this to be in the key, I can just plot a line with lt 0 but specify the y-value as 1/0. This will insert it in the key, but will not actually draw the line.
plot [-3:3] x**2 t "X Squared", 1/0 t "Vertical Line" lt 0

how to split the key in gnuplot?

I have plotted this table in gnuplot. as you can see the key should be seperated in two parts. The first 4 keys should remain in their place but the rest 4 keys should move down so that they won't cross the data.
How can I split the key this way?
One plot only supports a single key, so it can not be split. Also, all data is plotted by one single plot command, so you can not plot the first half of the data, change settings of the key and that plot the other half.
There are other ways to place the key, e.g. outside the plot area.
However, I have two workarounds:
Dummy plots
First, you can add dummy plots:
plot \
sin(x),
"+" u 1:(NaN) title " " w dots linecolor rgb "white",
"+" u 1:(NaN) title " " w dots linecolor rgb "white",
cos(x) title "cosinus",
tan(x) title "tangens"
Due to the 1:(NaN), no data is plottet. The single dot in the legend is white, and the title is a white space. So, it looks like empty lines in the key:
Multiplot
The other solution is to create two plots using multiplot:
set xrange[...]
set yrange[...]
set multiplot
plot sin(x)
set key bottom right
plot cos(x) linetype 2
unset multiplot
note that you have to set the ranges explicitly here. Also, axes, tics and labels are drawn twice, which may look odd on some output formats. In this case, you can unset all of them before the second plot, so everything is drawn only once.
While this way is a bit more complicated, you have much more control over your key:

Gnuplot, Multiplot: Mix normal and parametric plot with a single legend box?

I create overlapping graphs in Gnuplot, because I mix normal and parametric plots (and also pm3d maps and parametric surfaces). This works fine mostly, except for one thing: If both plots have a title, the legends usually overlaps. A typical example looks like this:
#legends.gp
set term pngcairo enhanced color linewidth 1.5 dashed dashlength 1.4 rounded
set output "legends.png"
set title "legends test"
set multiplot
# make a box around the legend
set key box
set border 15 lw 1
# fix the margins, this is important to ensure alignment of the plots.
set lmargin at screen 0.15
set rmargin at screen 0.98
set tmargin at screen 0.90
set bmargin at screen 0.15
set xlabel "x"
set ylabel "sin(x)"
set xrange[0:2*pi]
set yrange[-1:1]
set grid x y
# add single tic at 0.62
set xtics add ("x0" 0.62)
# main plot command
plot sin(x) title "sinus"
# turn everything off
set format x "" #numbers off
set format y ""
set xlabel "" #label off
set ylabel ""
set border 0 #border off
unset xtics #tics off
unset ytics
unset grid #grid off
unset title #title off
#plot vertical line at 0.62
set parametric
plot 0.62,t ls 2 lw 2 title "parametric Line"
unset parametric
unset multiplot
My question is now, is there a simple, mostly automatic way to create a single legend for multiple plots?
P.S. Sorry, I ended up making the example file more complex than it had to be by showing some more features, that are hopefully helpful for future readers.
Here's a VERY dirty hack that works for me. change:
plot sin(x) title "sinus"
to:
plot sin(x) title "sinus",NaN w l ls 2 lt 2 title "parametric line"
Then plot the parametric line without a title (e.g. notitle instead of title "parametric line").
This works because gnuplot ignores NaN's when plotting -- Essentially the second thing we're plotting above just adds one element to the legend. I specify the linetype, etc to be the same as your parametric plot linestyle/type so that it shows up properly in the legend. To my knowledge, this is the only way to do something like this...
Of course, you could just edit it so that both are plotted parametrically and forgo the entire multiplot buisness...
set xrange [0:2*pi]
set yrange [-1:1]
set parametric
set trange [-10:10]
plot t,sin(t) title "Hello", 0.62,t title "World"
that's probably the "cleaner" solution...(but less fun working with gnuplot "magic")
From the gnuplot info manual:
To draw a vertical line from the bottom to the top of the graph at
x=3, use:
set arrow from 3, graph 0 to 3, graph 1 nohead

Resources