- Set pmLegendDisplayMode = "Always". (Examples 1, 2, 3, or 4.)
- Create a common legend for use in paneled plots. (Example 5.)
- Use gsn_legend_ndc. (Example 6.)
- Make individual calls to gsn_polymarker_ndc and gsn_text_ndc. (Example 7.)
- Use gsn_create_legend. (Example 8.)
- Use simple_legend. (Examples 16 and 17.)
pmLegendDisplayMode = "Always"
Turns on the legend. The location of the legend within the plot is
controlled primarily by three resources. One chooses the reference
axis (pmLegendSide = "Top") and
the other two determine in NDC coordinates how far to move the legend
perpendicular (pmLegendOrthogonalPosF = -0.3 ) and
parallel (pmLegendParallelPosF =
.1) to this reference axis.
In this example the top axis is the reference, and the Parallel value of .1 keeps the legend close to the left axis. Increasing this value will move the legend at the same "height" across the plot to the right.
Note that the orthogonal position is negative. This is expected. In this case decreasing values (e.g. from -0.3 to -0.4) would move the legend down the plot.
The default size of the legend labels is also too large, and so is scaled down with lgLabelFontHeightF = 0.03.
The values of the legend labels are determined by:
xyExplicitLegendLabels = (/"U","V"/). The default value is the name of the variable.
Finally, the default box around the legend is turned off with:
lgPerimOn = False
lgOrientation = "horizontal"
changes the orientation of the legend.
lgLabelAngleF = 280., Changes the angle of the labels.
A legend can have two types of labels. One is in the center of the line lgLineLabelStrings, and the other is at the end lgLabelStrings. This example uses the former, which is why several of the resources are different from the other legend examples.
gsn_legend_ndc is the function that will add a legend to a contour plot.
If you add other plot items, like grid lines (see 2nd and 3rd images), then you need to draw the plot twice in order to get the legend to mask the additional items.
Setting xyLabelMode = "Custom" allows you specify labels for the XY curves. These labels will automatically be used in the legend.
The following XY resources translate into the following legend resources:
moving it into the plot (pmLegendOrthogonalPosF / pmLegendParallelPosF)
changing the legend width and height (pmLegendWidthF / pmLegendHeightF)
increasing the label size (lgLabelFontHeightF)
decreasing the length of the legend lines (lgBoxMinorExtentF)
This script has a function called "create_legend_from_plot" for creating and attaching the legend. It uses special attributes attached to the "opt" variable to indicate how many legend items are desired.
The steps are 1) create the first plot with two curves and no legend, 2) create the second plot with the other two curves and with a legend, 3) call overlay to overlay the two plots, 4) draw the overlaid plots.
This example is slightly different in that it shows how to draw legends on top of tickmarks; there's no draw order resource for this (yet).
This example creates a simple XY plot and attaches the legend to the upper left corner of the plot by setting a few of the resources that the function accepts. Note that draw and frame must be called after the legend is created.