Next: label, Previous: isosamples, Up: set-show
The key enables a key (or legend) describing plots on a plot.
The contents of the key, i.e., the names given to each plotted data set and function and samples of the lines and/or symbols used to represent them, are determined by the `title` and with options of the {`s`}`plot` command. Please see `plot title` and with for more information.
Syntax:
set key {on|off} {default} {left | right | top | bottom | outside | below | <position>} {Left | Right} {{no}reverse} {samplen <sample_length>} {spacing <vertical_spacing>} {width <width_increment>} {height <height_increment>} {{no}autotitles} {title "<text>"} {{no}enhanced} {{no}box { {linestyle | ls <line_style>} | {linetype | lt <line_type>} {linewidth | lw <line_width>}}} unset key show key
By default the key is placed in the upper right corner of the graph. The keywords `left`, `right`, `top`, `bottom`, `outside` and `below` may be used to place the key in the other corners inside the graph or to the right (outside) or below the graph. They may be given alone or combined.
Plots may be drawn with no visible key by requesting `set key off` or key.
Justification of the labels within the key is controlled by `Left` or `Right` (default is `Right`). The text and sample can be reversed (`reverse`) and a box can be drawn around the key (`box {...}`) in a specified `linetype` and `linewidth`, or a user-defined `linestyle`. Note that not all terminal drivers support linewidth selection, though.
The length of the sample line can be controlled by `samplen`. The sample length is computed as the sum of the tic length and <sample_length> times the character width. `samplen` also affects the positions of point samples in the key since these are drawn at the midpoint of the sample line, even if the sample line itself is not drawn.
The vertical spacing between lines is controlled by `spacing`. The spacing is set equal to the product of the pointsize, the vertical tic size, and <vertical_spacing>. The program will guarantee that the vertical spacing is no smaller than the character height.
The <width_increment> is a number of character widths to be added to or subtracted from the length of the string. This is useful only when you are putting a box around the key and you are using control characters in the text. `gnuplot` simply counts the number of characters in the string when computing the box width; this allows you to correct it.
The <height_increment> is a number of character heights to be added to or subtracted from the height of the key box. This is useful mainly when you are putting a box around the key, otherwise it can be used to adjust the vertical shift of automatically chosen key position by <height_increment>/2.
All plotted curves of `plot`s and `splot`s are titled according to the default option `autotitles`. The automatic generation of titles can be suppressed by `noautotitles`; then only those titles explicitly defined by `(s)plot ... title ...` will be drawn.
A title can be put on the key (`title "<text>"`)—see also `syntax` for the distinction between text in single- or double-quotes. The key title uses the same justification as do the plot titles.
An explicitly given title is typeset using enhanced text properties on terminals supporting this, see `enhanced text` for more details. This default behavior can be switched off by the `noenhanced` option.
The defaults for key are `on`, `right`, `top`, `Right`, `noreverse`, `samplen 4`, `spacing 1.25`, `title ""`, and `nobox`. The default <linetype> is the same as that used for the plot borders. Entering `set key default` returns the key to its default configuration.
The <position> can be a simple x,y,z as in previous versions, but these can be preceded by one of four keywords (`first`, `second`, `graph`, `screen`) which selects the coordinate system in which the position of the first sample line is specified. See `coordinates` for more details.
The key is drawn as a sequence of lines, with one plot described on each line. On the right-hand side (or the left-hand side, if `reverse` is selected) of each line is a representation that attempts to mimic the way the curve is plotted. On the other side of each line is the text description (the line title), obtained from the `plot` command. The lines are vertically arranged so that an imaginary straight line divides the left- and right-hand sides of the key. It is the coordinates of the top of this line that are specified with the key command. In a `plot`, only the x and y coordinates are used to specify the line position. For a `splot`, x, y and z are all used as a 3-d location mapped using the same mapping as the graph itself to form the required 2-d screen position of the imaginary line.
Some or all of the key may be outside of the graph boundary, although this may interfere with other labels and may cause an error on some devices. If you use the keywords `outside` or `below`, `gnuplot` makes space for the keys and the graph becomes smaller. Putting keys outside to the right, they occupy as few columns as possible, and putting them below, as many columns as possible (depending of the length of the labels), thus stealing as little space from the graph as possible.
When using the TeX or PostScript drivers, or similar drivers where formatting information is embedded in the string, `gnuplot` is unable to calculate correctly the width of the string for key positioning. If the key is to be positioned at the left, it may be convenient to use the combination `set key left Left reverse`. The box and gap in the grid will be the width of the literal string.
If `splot` is being used to draw contours, the contour labels will be listed in the key. If the alignment of these labels is poor or a different number of decimal places is desired, the label format can be specified. See clabel for details.
Examples:
This places the key at the default location:
set key default
This disables the key:
unset key
This places a key at coordinates 2,3.5,2 in the default (first) coordinate system:
set key 2,3.5,2
This places the key below the graph:
set key below
This places the key in the bottom left corner, left-justifies the text, gives it a title, and draws a box around it in linetype 3:
set key left bottom Left title 'Legend' box 3