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
Related
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
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 have four curves to plot. The first three are variants of each other, the fourth one is distinct. Hence, I would like the key to split in 3+1. However, using e.g.
set key maxrows 3
plot sin(x),sin(2*x),sin(3*x),exp(x)
gives a two-row key.
Can I force gnuplot to split the key in three+one?
In addition to #user8153's solution, I would like to suggest a slightly more general solution.
What if you have a colored background? For example: set term wxt background rgb "grey90" or if
you have a colored key (or legend) box... hmm, well, gnuplot doesn't offer an option for
colored background of the key box. Well, then if you put a colored rectangle behind the key.
Of course you can always adapt the color to the background, but I guess it would be simpler to plot an invisible line with lt -2 or lt nodraw.
About lt -2 or lt nodraw, I just learned a few days ago here: gnuplot: why is linewidth 0 not zero in width?. It's not (yet) in the manual, although it seems to be around since gnuplot version 5.0.
Code:
### invisible keyentry for column/row arrangement
reset session
set key top left maxrows 3
set obj 1 rect from graph 0.02,0.82 to graph 0.52,0.98 fs solid 1.0 fc rgb "grey90"
set xrange[-2:2]
set yrange[-2:2]
plot sin(x), sin(2*x), sin(3*x), exp(x), NaN lt -2 ti " "
### end of code
Result:
Addition:
Actually, I forgot that there is a workaround for a colored background of the keybox. No manual adjustment and fiddling around with the box:
Set custom background color for key in Gnuplot
In case you want a transparent background, e.g. for images on webpages using, e.g. pngcairo, pdfcairo, etc., besides lt -2 or lt nodraw, another solution would be to plot a transparent line, e.g. transparent "black" lc rgb 0xff000000:
plot sin(x), sin(2*x), sin(3*x), exp(x), NaN lc rgb 0xff000000 ti " "
Code:
### invisible keyentry for column/row arrangement with transparent background
reset session
set term pngcairo transparent
set output "tbKeyRows.png"
set key top left maxrows 3
set xrange[-2:2]
set yrange[-2:2]
plot sin(x), sin(2*x), sin(3*x), exp(x), NaN lt -2 ti " "
set output
### end of code
Result: (shown in front of checkerboard pattern)
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.)
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