gnuplot: legend gets hidden behind data - gnuplot

I am new to gnuplot and while plotting stacked histogram, I find that legend gets hidden behind the data.
Is there a method to put the legend above the data? Thanks a lot for your help.
EDIT: I am currently using set key outside bottom to place legend outside, but that is not the best solution I would like.

Recent versions allow to make the background of the legend white:
set key opaque
This simply adds a white background to the legend so it appears on top of all graphs. Found the answer in this Post.

If you would rather have the key on top of the data rather than the key outside the plot box altogether, here is one workaround (using sin(10*x) as an example):
set multiplot
unset key
plot sin(10*x) # this will plot with tics and a border, but no key
set key box
unset tics
unset border
bignumber=10 # make this number larger than the y range so the line will not be seen
plot [][0:1] bignumber title 'sin(10*x)' # this will just plot the key with the title
unset multiplot
Using this method, first you plot your data/function, then you create a plot on top of that which just has a key. You have to make sure to set the title of the second plot properly.

Related

How to show a grid with two filled plots

I am plotting to datasets with 'fillsteps' one below another and I want the plot two show only the area that is a difference between the two
plot [0:1][0:1] x with fillsteps above fill solid not,x**2 with fillsteps above fill solid lc rgb 'black' not
But the grid obviously gets blocked in this case:
Is there any way to create something like a cross-section between the two areas, show the grid and get rid of those nasty artifacts that are seen below?
Ok, basically you want to fill the area between two curves (either lines or steps) and have a grid on top. The set grid front you found yourself, but let me make another suggestion.
For the first case (lines), i.e. a fill between lines, you can simply use 3 columns (check help filledcurves) and then the area will be filled between the curves:
plot '+' u 1:(f1(x)):(f2(x)) w filledcurves
For the second case (steps), I don't see (yet) such an option with filledsteps. Actually, from your option above I assume you are using gnuplot5.5.
In general, I wouldn't call it a "clean" solution if you plot something and partly have to cover it with something else with background color. What if you want a transparent background? A transparent color which covers something colored has not yet been invented ;-), there is no such "invisible" color. For another example check this.
Furthermore, with fillsteps I can also observe the artifacts of vertical gap lines which you see in your graph, but I don't have a good solution to avoid them.
Hence, my suggestion is to plot only there where you need something to plot. Actually, you can mimic the fillsteps behaviour. It's not obvious how to do it, but not too difficult. While you plot line by line, you remember the previous x-value and function values of f1(x0) and f2(x0) in x0, y0 and y2, respectively. You plot with the plotting style boxxyerror (check help boxxyerror) using x:y:xlow:xhigh:ylow:yhigh.
Script: (works with at least gnuplot>=5.0.0)
### plotting style filledcurves and mimic fillsteps "between"
reset session
f1(x) = x
f2(x) = x**2
set xrange[0:1]
set yrange[0:1]
set key noautotitle
set grid x,y front lw 1.3
set style fill solid 1.0 border
set samples 50
set multiplot layout 2,1
plot '+' u 1:(f1(x)):(f2(x)) w filledcurves
plot x1=y1=y3=NaN '+' u (x0=x1,x1=$1):(y0=y1,y1=f1($1),y2=y3,y3=f2($1)):(x0):(x1):\
(y0):(y2) w boxxy
unset multiplot
### end of script
Result: (download the PNG image and check that the background is transparent).
set grid front
works in this case

How can I split my key so that one part is top right and the other part is top left?

since my last post I tried cleaning up my code a bit and am almost done with the plot. The only thing left to do is splitting the key of the first plot into two parts. One being the titles of the graphs and the other one being the time that data was captured. The title should go top left while the time should go top right.
The code:
set key top left title '0 s'ยด
set ylabel 'Konzentration'
set format y '%g'
set ytics border out nomirror 2
set mytics 2
plot 'data/paper_2_csv_a_0000.csv' using 3:1 axes x1y1 with lines ls 1 title 'Aktivator',\
'data/paper_2_csv_h_0000.csv' using 3:1 axes x1y1 with lines ls 2 title 'Inhibitor'
;
produces the output
So how can I move the "Aktivator" and "Inhibitor" part to the left while leaving the "0 s" where it is right now?
There are several titles:
a title of a plot, check help title
a title of a legend/key, check help key, option title
a title of a multiplot, check help multiplot, option title
I guess you want to separate the key title and move it to the other side. I guess that's not possible.
Probably the easiest is to place a label. You place it e.g. at a position relative to the graph (here: 0.95,0.95) and the text alignment at that position is right. Check help label.
Labels are persistent (e.g. in a multiplot environment), so don't forget to set unset label 1 or overwrite it with another title for the next multi(sub)plot.
Script:
### place a label/title
reset session
set key top left
set label 1 "0 s" at graph 0.95,0.95 font ",12" right
set offsets 0,0,0.2,0.2
plot sin(x), cos(x)
### end of script
Result:

Gnuplot: align key directly under xlabel?

I'm am plotting multiple variables in gnuplot. I would like to centre the key under the plot, and centred on the plot area.
My current settings are:
set key outside
set key bottom center horizontal
This centres the key across the entire width of the plot. I would like to centre the key across the extent of the x axis; ie align it with the xlabel.
Is this possible in an automated fashion, or is tweaking involved?
Edit: This is all my mistake. I had asked gnuplot to plot a file that wasn't there, so its key was empty, but the space for it was still there, and that threw off the "alignment"; it was aligned with three entries, but not for the visible two.
Depending on which arrangment and alignment you are looking for...
also check help key for more options.
reset session
set multiplot layout 2,1
set xlabel "This is the xlabel"
set key outside bottom center vertical
plot sin(x), cos(x), sin(-x), cos(-x)
set key outside bottom center horizontal
replot
unset multiplot

Output gnuplot legend in separate file [duplicate]

I have an interactive perl script which uses data from mysql to generate many plots through the Chart::Gnuplot package. There are times when the graph size is overloaded with too many plots.
I would option to generate the gnuplot image containing only the legend (no graph).
I don't know if this would help, but...
plot [0:1] [0:1] NaN title "Hello" #Just the label in the legend.
or...
plot sin(x),NaN title "Boo" #Plots sin(x) (properly labelled) and a second label "Boo"
Of course, this still has the border and other things. You can unset those...unset border and unset tics

Plot two datasets on the same graph with gnuplot. One with dgrid3d, the other one without

I'm trying to plot two data sets with gnuplot. They are both (x, y, z) triplets. They are not arranged on a grid. I want to plot one of them using dgrid3d and pm3d. On top of that I want to overlay the other data set but as just scattered points.
To give a more concrete example: I am trying to plot the effect of a cylinder approaching a surface. I want to plot the response of the surface and that's where dgrid3d comes in handy. On top of that, I want to plot the position of the cylinder and I have its circumference as points.
I used:
set dgrid3d 100,100,4
set pm3d
splot "dataset1" with pm3d, "dataset2" with dots
The data set has about 100x100 points, arranged on a near-square, so 100,100 works best here. No matter how I plot the second data set, it always ends up being a square of the same dimensions as the cylinder, instead of a nice circle. When I turn dgrid3d off, I can plot the second data set on its own and the result is a nice circumference of the cylinder.
So my question is: is it possible to plot a 3D graph using two data sets, one using dgrid3d and the other one not using it?
Yes, this is possible, but it is a little more tricky than you might think. The key is to use set table
For your example:
set dgrid3d 100,100,4
set pm3d explicit
set table "interpolated_data.dat"
splot "dataset1" with pm3d #writes the interpolated data to "interpolated_data.dat"
unset table
unset dgrid3d
splot "interpolated_data.dat" with pm3d, "dataset2" with dots
The reason that your attempt didn't work is because when dgrid3d is in effect, All data read in is interpolated to a grid and then plotted using whatever style you specify.
From gnuplot's help dgrid3d
When enabled, 3D data read from a file
are always treated as a scattered data set.
As a side note, this method can also be used to plot contours on top of a pm3d as well.

Resources