how to split the key in gnuplot? - 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:

Related

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

plotting two histograms, one w/ and one w/o error bars in one plot

I have the below script, which works fine when I have a third column in the second data set. Now I want to get the first histogram being drawn w/ error bars, and the second w/o. I can remove the :3 from the second plot command but gnuplot will complain about not enough data specified for the second histogram. If I remove set style histogram errorbars ... but that would disable the error bars on the first histogram, too. Is there a way to plot two histograms in the same figure, where one doesn't have error bars.
set xlabel ""
set ylabel ""
set boxwidth 0.9 absolute
set style fill solid 1.00 border -1
set style histogram errorbars gap 1
set style data histograms
set yrange [-1.746917959031165368e-01:3.668527713965446857e+00]
unset key
set datafile commentschar "#"
plot '-' using 2:3:xtic(1) title "onehist",\
'-' using 2:3:xtic(1) title "otherhist"
-3.583733737468719482e-01 1.073847990483045578e-02 1.073847990483045578e-02
-3.382162153720855713e-01 2.274234220385551453e-02 1.329828426241874695e-02
2.261839509010314941e-01 2.859487235546112061e-01 8.173441886901855469e-02
e
-1.164875924587249756e-01 4.266476333141326904e-01
-9.633044153451919556e-02 5.953223109245300293e-01
-7.617329061031341553e-02 6.151663661003112793e-01
-5.601614341139793396e-02 9.624376893043518066e-01
e
I'm not sure if it is possible to do this generally, but you can draw your histograms without the errorbars and then add them afterwards with an additional plot command.
plot '-' using 2:xtic(1) title 'onehist',\
'-' using ($0-0.2):2:3 with yerrorbars lc 'black' pt 0, \
'-' using 2:xtic(1) title 'otherhist',\
I'm not entirely sure how to determine the range of the actual bars, so the error bars are not perfectly centered, but this will place them on your graph as requested.
The additional command uses the yerrorbars style (which is how the histogram bars are drawn) to draw the error bars.
However, this isn't the best way to draw histograms. Gnuplot will treat the x-axis as a category with values 0, 1, 2, 3, etc. Therefore, even though you have different x values in both of your lists above, they will become superimposed over each other (and the second plot will change the x-axis values set by the first).
For your example, I would recommend using the boxes and boxerrorbars style.
set style fill solid
set boxwidth 0.01
plot '-' using 1:2:3 with boxerrorbars, '-' u 1:2 with boxes
or if you need the error bars to be a different color, draw them separately
plot '-' using 1:2 with boxes,\
'-' using 1:2:3 with yerrorbars lc 'black' pt 0,\
'-' u 1:2 with boxes

Gnuplot histogram gap does nothing

I have a gnuplot script which plots a histogram. I used the following syntax:
set style data histogram
set style histogram cluster gap 2
set style fill solid
set logscale y
rgb(r,g,b) = int(r)*65536 + int(g)*256 + int(b)
plot 'histogram_data' using (column(0)):2:(0.5):(rgb($3,$4,$5)):xticlabels(1) w boxes notitle lc rgb variable
What the last line does is: using column 1 as x labels, column 2 as the height of the histogram bars, 0.5 as box width, and columns 3, 4 and 5 as the rgb values to colour the bars.
Now, the problem is that modifying the gap parameter in line 2 does not change in any way the spacing between bars, even though as far as I understand that is the correct way to adjust such spacing. I am using gnuplot 4.6 patchlevel 4.
I found a way to do this with boxes, though I do not consider it very clean:
plot 'histogram_data' u (column(0)*2+1):2 w boxes notitle lc rgb 'white',\
'histogram_data' u (column(0)*2):2:(rgb($3,$4,$5)):xticlabels(1) w boxes notitle lc rgb variable;
This command is plotting all the data of the main plot on even slots and a white box on odd slots. So the first line in the plot command is plotting the gaps between every box of the plot (the width of these gaps can be specified using the boxwidth property I think but I haven't tested this), while the second line is drawing the actual plot.
I could not find a way to do this with the histogram plotting style, keeping the variable colours specified in the data file.

Reduce distance between points in splot

I have this gnuplot script
reset
set palette model RGB defined (0 "gray", 0.1 "white", 0.33 "yellow", 0.66 "orange", 1 "red")
set xlabel "x"
set ylabel "y"
set view map
set border 0
unset xtics
unset ytics
splot file_name u 1:2:5:xtic(3):ytic(4) w points ps 5 pt 5 palette
And this is the result:
How can I remove the distance between the points so that I end up with a set of adjacent squares? I want to plot a heatmap with a square for each point in my grid file.
EDIT
The correct way to plot a "grid" heatmap as per #andyras answer is:
set pm3d map
plot file_name u 1:2:5:xtic(3):ytic(4) with image
Which gives this image:
gnuplot offers a third way to plot a heatmap based on connecting points of a 3d surface, rather than grid cells. That is, the x,y coordinates at columns 1,2 are used as corners, or connecting points, of a surface mesh and the colors used in each region are the average of the RGB/HSV values for the 4 defining corners:
set pm3d map
splot file_name u 1:2:5:xtic(3):ytic(4)
I usually go with the options
set pm3d map
plot file_name u 1:2:5:xtic(3):ytic(4) with image
for gridded data (it makes a smaller file if you use vector formats). I suspect your problem may be to do with the fact that you specify a point style and size for your splot. So, you could try setting the pm3d map option and using splot without the point specification, or plot ... with image.

bring legend to the front in 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

Resources