NCL Home> Application examples> Plot techniques

Legends

There are five distinct ways to create legends, and all are shown in the examples below.
leg_1.ncl: The resources in this script contain the bare minimum to get a two item legend in the upper left corner of the plot. The default values of many of these resources draw a huge legend that is outside of the plot domain.

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

leg_2.ncl: Adds a title and bounding box to the plot. Changes the position of the legend.

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

leg_3.ncl: Demonstrates what a legend looks like with colored markers. Swaps the orientation.

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

leg_4.ncl: In many cases there may be very little space on a line plot to place a label, particularly when there are many lines and the label is long. This examples demonstrates making the legend horizontal, using the height and width attributes to really shrink the lines, and then angling the labels in order to shrink it even more.

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.

leg_5.ncl: Demonstrates the old way of creating a custom legend. See example 6 for an easier method.

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.

leg_6.ncl: Demonstrates the addition of a legend for a contour-on-contour plot.

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

leg_7.ncl: Demonstrates how to add a legend using calls to gsn_polymarker_ndc and gsn_text_ndc.
leg_8.ncl: Demonstrates how to create a legend by using gsn_create_legend, available in NCL version 4.2.0.a034. After the legend is created you have to attach it to the plot by using gsn_add_annotation.

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

leg_9.ncl: Demonstrates how to change the order of the items in a legend using the lgItemOrder resource, added in version 5.1.0.
leg_10.ncl: This example demonstrates how to fill the legend box so that it masks the XY curves, by setting lgPerimFill to "SolidFill" and lgPerimFillColor to the desired fill color.

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.