- 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, 17 and 19.)
- Use simple_legend_ndc. (Examples 20 and 21.)

NCL Graphics: Legends


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 is much to large for the plot, so
several resources are set to resize the legend:
pmLegendWidthF = 0.15
pmLegendHeightF = 0.18.
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
A Python version of this projection is available here.

lgTitleOn = True
lgTitleString = "Example"
lgTitleFontHeightF = .025, Turns on the title and
sets its value and font size.

lgOrientation = "horizontal"
changes the orientation of the legend.

lgLabelAngleF = 280., Changes the angle of the labels.
pmLegendWidthF = 0.12
pmLegendHeightF = 0.05
lgLabelFontHeightF = .02
were the values
I chose to make this legend small. You can experiment with values that
work for you.

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.


This example requires NCL V6.5.0 to run.


gsn_legend_ndc is the function that will add a legend to a contour plot.

Note when using gsn_add_annotation that gsnDraw and gsnFrame must be set to False. This results in the need to call draw and frame after the legend is attached to the 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.
NOTE: in NCL V6.5.0, a new tmGridDrawOrder resource was added that allows you to control which phase the grid lines are drawn in ("PreDraw", "Draw", "PostDraw"). See the leg_650_10.ncl script.
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:
xyMarkLineModes | lgItemTypes |
xyDashPatterns | lgDashIndexes |
xyMarkers | lgMarkerIndexes |
xyMarkerColors | lgMarkerColors |
Note that you have to set lgAutoManage to False in order to be able to control the font height of the legend title (lgTitleFontHeightF).


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)


In addition, this example also shows how to customize the legend labels and line length.

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 script also shows how to create legend entries by selecting and reversing data for XY curves.

This example is slightly different in that it shows how to draw legends on top of tickmarks. In NCL V6.5.0, you can use the tmGridDrawOrder resource to accomplish this.

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.


This resource was added in NCL V6.5.0. If you try to run this script with NCL V6.4.0 or earlier, the grid lines will show up on top of the legend and you will get a warning:
tmGridDrawOrder is not a valid resource in leg_xy at this time



