## Key

The set key command enables a key (or legend) containing a title and a sample (line, point, box) for each plot in the graph. The key may be turned off by requesting set key off or unset key. Individual key entries may be turned off by using the notitle keyword in the corresponding plot command. The text of the titles is controlled by the set key autotitle option or by the title keyword of individual plot and splot commands. See plot title (p. ) for more information.

Syntax:

         set key {on|off} {default}
               {{inside | outside} | {lmargin | rmargin | tmargin | bmargin}
                 | {at <position>}}
               {left | right | center} {top | bottom | center}
               {vertical | horizontal} {Left | Right}
               {{no}opaque}
               {{no}reverse} {{no}invert}
               {samplen <sample_length>} {spacing <vertical_spacing>}
               {width <width_increment>} {height <height_increment>}
               {{no}autotitle {columnheader}}
               {title "<text>"} {{no}enhanced}
               {font "<face>,<size>"} {textcolor <colorspec>}
               {{no}box {linestyle <style> | linetype <type> | linewidth <width>}}
               {maxcols {<max no. of columns> | auto}}
               {maxrows {<max no. of rows> | auto}}
         unset key
         show key

Elements within the key are stacked according to vertical or horizontal. In the case of vertical, the key occupies as few columns as possible. That is, elements are aligned in a column until running out of vertical space at which point a new column is started. The vertical space may be limited using 'maxrows'. In the case of horizontal, the key occupies as few rows as possible. The horizontal space may be limited using 'maxcols'.

By default the key is placed in the upper right inside corner of the graph. The keywords left, right, top, bottom, center, inside, outside, lmargin, rmargin, tmargin, bmargin (, above, over, below and under) may be used to automatically place the key in other positions of the graph. Also an at position may be given to indicate precisely where the plot should be placed. In this case, the keywords left, right, top, bottom and center serve an analogous purpose for alignment. For more information, see key placement (p. ).

Justification of the plot titles within the key is controlled by Left or Right (default). 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.

By default the key is built up one plot at a time. That is, the key symbol and title are drawn at the same time as the corresponding plot. That means newer plots may sometimes place elements on top of the key. set key opaque causes the key to be generated after all the plots. In this case the key area is filled with background color and then the key symbols and titles are written. Therefore the key itself may obscure portions of some plot elements. The default can be restored by set key noopaque.

By default the first plot label is at the top of the key and successive labels are entered below it. The invert option causes the first label to be placed at the bottom of the key, with successive labels entered above it. This option is useful to force the vertical ordering of labels in the key to match the order of box types in a stacked histogram.

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 and want larger borders around the key entries.

All plotted curves of plots and splots 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.

The command set key autotitle columnheader causes the first entry in each column of input data to be interpreted as a text string and used as a title for the corresponding plot. If the quantity being plotted is a function of data from several columns, gnuplot may be confused as to which column to draw the title from. In this case it is necessary to specify the column explicitly in the plot command, e.g.

         plot "datafile" using (($2+$3)/$4) title columnhead(3) with lines

An overall title can be put on the key (title `"`text`"`) -- see also syntax (p. ) for the distinction between text in single- or double-quotes.

The defaults for set key are on, right, top, vertical, Right, noreverse, noinvert, 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 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 set 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 3D location mapped using the same mapping as the graph itself to form the required 2D screen position of the imaginary line.

When using the TeX or other terminals where formatting information is embedded in the string, gnuplot can only estimate the correctly exact 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.

If splot is being used to draw contours, by default the contour labels will be listed in the key. You can adjust this display using set cntrlabel format.

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 at 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

Subsections

- Key placement
- Key samples