I have this "MWE"
set title font "Monospaced,13" 'Total duration'
set grid
set style data histograms
set style histogram cluster
set style fill solid
set xtics format ""
set grid ytics
set ylabel "Milliseconds"
AddFirstColor = "#491d75";
AddLastColor = "#b32929";
RemoveFirstColor = "#d49435";
RemoveLastColor = "#12520b";
set yrange[0:16000]
set ylabel "Milliseconds"
set key at 1.9,2.5 opaque box lc "black" linewidth 3 spacing 1.2 box opaque fillcolor "grey"
plot 'data.dat' using 2:xtic(1) title "Add first" linecolor rgb AddFirstColor, \
'' using 3 title "Add last" linecolor rgb AddLastColor, \
'' using 4 title "Remove first" linecolor rgb RemoveFirstColor, \
'' using 5 title "Remove last" linecolor rgb RemoveLastColor, \
'' u 0:1:1 with labels offset -6.0,-100.0 title ""
set terminal png size 650,350 enhanced font "Monospaced,13"
set output 'data.png'
replot
exit
The data.dat
contains:
# AddFirst AddLast RemoveFirst RemoveLast
"Linked list" 6841 6675 1189 1042
"Circular list" 7037 9703 2546 2332
The output is:
I wish to place the key box on top right such that it is (say) 5 pixels from the top line of the plot and 5 pixels from the right verical line. How could I achieve that?
Edit: extending the question
Now I have two graphs:
AddAtBeginning.png
:
and
As you can see, the key box is anchored to the left top corner differently in the above two graphs: the horizontal distances from the key box and the y-axis do not match. I need to make them equidistant. (I suppose it is due to the fact that the two plots have the y-label area of different sizes, since we are dealing with different scales.) Also, it would be nice if the plot boxes had similar geometry (same dimensions, same position relative to entire .png file) (this might or might not solve my issue automatically).
AddAtBeginning.plt
is given by:
set xlabel 'Size'
set ylabel 'Microseconds'
set xrange [0:1000000]
plot 'AddAtBeginning.dat' index 0 title "Array list", \
'' index 1 title "Indexed list", \
'' index 2 title "Linked list", \
'' index 3 title "Tree list
set terminal png background rgb "grey" size 650,350 enhanced font "Monospaced,13"
set output 'AddAtBeginning.png'
set style rect fc rgb "white" fs
set object 1 rectangle from screen 0,0 to screen 1,1 behind
set key at graph 0.425,0.97 box lt -1 lw 2 opaque
replot
exit
Also, AddAtEnd.plt
is given by:
set xlabel 'Size'
set ylabel 'Microseconds'
set xrange [0:1000000]
plot 'AddAtEnd.dat' index 0 title "Array list", \
'' index 1 title "Indexed list", \
'' index 2 title "Linked list", \
'' index 3 title "Tree list
set terminal png background rgb "grey" size 650,350 enhanced font "Monospaced,13"
set output 'AddAtEnd.png'
set style rect fc rgb "white" fs
set object 1 rectangle from screen 0,0 to screen 1,1 behind
set key at graph 0.425,0.97 box lt -1 lw 2 opaque
replot
exit
In the above image, I need to anchor the red area statically to the entire .png
file (the green box) dimensions, and I need to anchor the violet key box statically relative to the red graph border.
Please always check gnuplot help before asking questions! There is to almost every keyword an entry help <keyword>
.
Here, it would have been help key
or in newer versions help key placement
.
There are different coordinate systems, check help coordinates
, but as far as I know no pixel coordinate system.
But something like the following should do the job:
set key at graph 0.995,0.99
Addition:
That's what I finally understood from your revised question: you want place the key box in a fixed (pixel) distance from a corner of a graph, independent of the graph size. If your x-range is the same for all graphs you could use first
for the x-position and graph
for the variable y-range.
Although, depending on the y-labels and y-tics, you could also set the same left margin and ensure the same size and aspect ratio for all plots. Then graph coordinates would be constant and independent of x- and y-range.
There is also a way to set it in a fixed pixel distance independent of graph size and x,y-ranges. However, this would be a cumbersome solution requiring replotting. If you're really interested let me know.
Script:
### place legend box at distance from corner "almost" independent of graph size
reset session
set key left box width 1
set key at first 10000, graph 0.97
set tics out
set multiplot layout 2,1
set xrange[0:1e6]
set yrange[0:10]
set ylabel "some label"
plot 1e-5*x, 2e-5*x, 3e-5*x w l
set yrange[0:1000000]
set ylabel "some multiline\nlabel\n with 3 lines" offset -1.5,0
plot x, 2*x, 3*x w l
unset multiplot
### end of script
Result:
Addition 2:
So, after all, it seems you were just looking for constant margins for all graphs, e.g. set margins 20,5,10,10
, check and read help margins
. If the graphs have the same size, the position of your key box will be identical if you use graph
coordinates. I hope, this will answer your question unless you have another unmentioned requirement.
Script:
### place legend box at distance from corner
reset session
set key left box width 1
set key at graph 0.01, graph 0.97
set tics out
set margins 20,5,10,10 # l,r,b,t
set multiplot layout 2,1
set xrange[0:1e6]
set yrange[0:10]
set ylabel "some label"
plot 1e-5*x, 2e-5*x, 3e-5*x w l
set yrange[0:1000000]
set ylabel "some multiline\nlabel\n with 3 lines" offset -1.5,0
plot x, 2*x, 3*x w l
unset multiplot
### end of script
Result: