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.)
Related
Consider the following example script :
reset session
set multiplot layout 1,2
set title "."
plot sin(x)
set title "."
plot sin(x**2)
unset multiplot
... one can see that the set title "." is a kludge for, ideally, a nicer dot - perhaps what is called a "bulletpoint". While I could pursue this kludge, using O, or other font sizes, point sizes, or {/Symbol } (in progress), or terminals of the LaTeX variety (because there are pretty good mathematical symbols I could use like cdot), it would be interesting to know in general - a simple shape like a dot or square, imposed somewhere on the plot, but outside the usual set title or label which have certain rules with the fonts and such.
It's still not fully clear to me what you want to achieve. Placing a point which tunable size as title or at the place where the title usually would be?
You can use set label with option point (check help label). You can set different point types, type test in the gnuplot console to see the available pointtypes. It's not clear, why you wrote that you don't want to use labels. Maybe with the example below we'll get a step further.
Script:
### place a point somewhere on the canvas
reset session
set multiplot layout 1,2
set title "."
plot sin(x)
set title " "
set label 1 at graph 0.2,1.05 point pt 7 ps 0.25 lc "dark-violet"
set label 2 at graph 0.3,1.05 point pt 7 ps 0.50 lc "blue"
set label 3 at graph 0.4,1.05 point pt 7 ps 0.75 lc "cyan"
set label 4 at graph 0.5,1.05 point pt 7 ps 1.00 lc "green"
set label 5 at graph 0.6,1.05 point pt 7 ps 1.25 lc "yellow"
set label 6 at graph 0.7,1.05 point pt 7 ps 1.50 lc "orange"
set label 7 at graph 0.8,1.05 point pt 7 ps 1.75 lc "red"
plot sin(x**2)
unset multiplot
### end of script
Result:
You can place a graphical object (drawn with vectors and/or solid fill) using the command set object {circle|rectangle|polygon} at .... See User Manual or interactive help for details.
You can use any unicode character including math symbols and even emojis in a title or label. Yes, you must have a font available that contains that character, but these days that is not much of a limitation. For example:
set title "Various solar symbols: ββΌπ£β"
Even if you have trouble entering the desired symbol via keyboard or cut-and-paste, current gnuplot allows you to specify the unicode symbol as an escape sequence:
set label 1 "Solar symbol" at graph 0.5, 1.0 point pointtype "\U+2609"
set border 3; unset tics
plot 1/exp(x) notitle
I gathered these approaches in a demonstration script the sin(x) functions are just for show :
reset session
unset key
#
set grid x,y
set style fill solid 1.0
set multiplot layout 2,2
#
set title "."
plot 1*sin(x)
unset title
#
set title "O"
plot 2*sin(x)
unset title
unset object
#
# series of objects
# only valid on on the specific plot area
# not outside the e.g. axes or border
set object circle at 1,1
set object circle at 1,2
set object circle at 1,3
plot 3*sin(x)
unset object
#
set label 1 at graph -0.1,1.10 point pt 7 ps 1.00 lc "black"
set label 2 at graph 0.0,1.10 point pt 7 ps 1.00 lc "black"
set label 3 at graph 0.5,1.05 point pt 7 ps 1.00 lc "black"
set label 4 at graph 0.5,1.10 point pt 7 ps 1.00 lc "black"
plot 4*sin(x)
unset label
#
unset multiplot
unset output
I couldn't get fill style solid 1.0 to work on set object circle the way I wanted so I left it, perhaps there is a way. One can see the circle gets cut off on the edge. The label coordinates also have requirements (see manual).
To be clear, I am writing this "answer" but the question is only "answered" because of theozh and Ethan (thanks!).
I would like to create a color bar plot of a single variable that draws a box to the left in red if the variable is negative and green to the right if positive.
I am failing to get rid of the y axis completely. There shall be no marking of it whatsoever. Second the x-axis and tics is hidden behind the box. I need it visible. Third the plot of the line at 0 is really unnecessary as I have already drawn all I need but gnuplot wants a plot cmd with some sort of argument. I tried plot 0 lt bgnd but that left an ugly white line in my box. I guess I can live with that. Arrows at the ends of the x-axis would be nice, too.
This is the current state of the code. (the variable v will later come from the outside world as command line argument)
v= 7.3
if (v<0){boxcolor= 'red'}
if (v>=0){boxcolor= 'green'}
unset border
unset ytics
unset key
set yzeroaxis
set xzeroaxis
set xtics axis
unset ytics
set xrange [-10:10]
set object 1 rect from 0.0,-0.5 to v,0.5 back fillcolor rgb boxcolor
plot 0
Result currently:
You are probably looking for something like this:
Update: improved version
using graph and first coordinates for the arrow (check help coordinates), hence independent of the actual x-range.
using xzeroaxis (check help xzeroaxis)
Script:
### only x-axis with arrows
reset session
set border 0
unset ytics
v= 7.3
boxcolor = (v<0) ? 'red' : 'green'
set xrange [-10:10]
set xtics axis mirror
set xzeroaxis lt 1 lc "black"
set object 1 rect from 0.0,-0.5 to v,0.5 behind fillcolor rgb boxcolor
set arrow 1 from graph -0.03, first 0 to graph 1.03, first 0 heads filled
plot cos(x)
### end of script
Result:
Another answer: This one uses the built-in axis variants rather than an arrow:
set border 0
unset key
# In newer gnuplot versions there is a keyword "nodraw"
# Here we define a synonym that works with older versions also
hide = -4
set yzeroaxis lt hide lc hide
set xzeroaxis lt black
set tics front
unset ytics
set xtics axis
# define rectangle here
set object 1 rect from 0,-.5 to 5,.5 behind fs noborder fc "green"
#
plot 0 with lines lc "black"
I want to plot two charts (reading the same data points) using multiplot. The purpose is to put the second (smaller) chart in the right of the first (bigger) chart.
The code is
set term post eps enhanced color blacktext size 4,4 solid "Times-Roman" 14
set output 'cla.ps'
set multiplot
# draw bottom and left lines
unset border
unset xtics
unset ytics
set border 3
# increasing the canvas size
set rmargin 50
set tmargin 2
# put the big chart, it will not use the whole space
set origin 0,0
set size 1.2,0.5
plot 'test.txt' u 1 with points lc rgb "black"
# put the small char in the right
set origin 0.6,0.2
set size 0.2,0.2
plot 'test.txt' u 1 with points lc rgb "black"
# always unset multiplot
unset multiplot
Problem is, the second chart is shown in a mirror fashion (horizontal flip). But I didn't such an option. How can I fix that?
That should give you a nice warning explaining that: warning: Terminal canvas area too small to hold plot. Check plot boundary and font sizes.
With set rmargin 50 you set the right margin to be 50 character widths. That applies also to the second plot unless you use e.g. set rmargin -1 to reset it to automatic calculation.
But I don't see any sense to set both the size and the rmargin. Setting the rmargin does not increase the canvas size, like you suggest in your comments.
Here is a working example:
set term post eps enhanced color blacktext size 4,2 solid "Times-Roman" 14
set output 'cla.ps'
set multiplot
# draw bottom and left lines
unset tics
set border 3
# put the big chart, it will not use the whole space
set origin 0,0
set size 0.8,1
plot x**2
# put the small char in the right
set origin 0.75,0.2
set size 0.25,0.3
plot x
# always unset multiplot
unset multiplot
That gives you the output (tested with 4.6.5):
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
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