4.6 Keys and Legends

By default, plots are displayed with legends in their top-right corners. The textual description of each dataset is drawn by default from the command used to plot it. Alternatively, the user may specify his own description for each dataset by following the plot command with the title modifier, as follows:

plot sin(x) title 'A sine wave'
plot cos(x) title ''

In the lower case, a blank title is specified, in which case PyXPlot makes no entry for the dataset in the legend. This is useful if it is desired to place some but not all datasets into the legend of a plot. Alternatively, the production of the legend can be completely turned off for all datasets using the command set nokey. The opposite effect can be achieved by the set key command.

The set key command command can also be used to dictate where on the plot the legend should be placed, using a syntax along the lines of:

set key top right

The following recognised positioning keywords are self-explanatory: top, bottom, left, right, xcentre and ycentre. The word outside places the key outside the plot, on its right side. The words below and above place legends below and above the plot respectively.

In addition, two positional offset co-ordinates may be specified after such keywords – the first value is assumed to be an $x$-offset, and the second a $y$-offset, both in units of centimetres. For example:

set key bottom left 0.0 -2

would display a key below the bottom left corner of the graph.

By default, entries in the key are placed in a single vertical list. They can instead be arranged into a number of columns by means of the set keycolumns command. This should be followed by the integer number of desired columns, for example:

set keycolumns 2

An example of a plot with a two-column legend is given in Figure 4.5.

\includegraphics{examples/eps/ex_legends.eps}
Figure 4.5: This plot shows how rapidly three functions, often approximated as $x$, deviate from that approximation. Furthermore it is an example of a plot with a two-column legend, positioned below the plot using set key below. The complete script used to produce the plot can be found on the PyXPlot website at http://www.pyxplot.org.uk/examples/Manual/07legends/.