Custom Legends for line plots
Question:
How can I customize the legend on a line plot with several lines?
Example:
! Draw a set of lines using the PLOT/ALONG= syntax use coads_climatology define view/x=0:0.9/y=0:1 view1 set view view1 ! If running PyFerret, set the font. IF ($program_name"|PyFerret>1|*>0") THEN SET TEXT/FONT=helvetica plot/along=x/L=5/x=120:200/color/thick sst[y=-40:0:10]
Ferret makes a default "legend" below a plot, with line samples and automatically-generated labels. It is accurate and descriptive but not for a final version of the plot! There are several "legend" scripts available which draw a set of lines on a page, generally within a corner of the plot. Type "Fgo legend" at the Unix command line to see a list of the available scripts.
This FAQ explores a more flexible method to draw and label a set of lines in a new viewport, and using the ANNOTATE command to label the lines.
Draw the main plot in a viewport, which has been defined to leave some space somewhere on the page. Here, we've left space at the right. First, we'll re-draw the line plot to replace the auto-generated key at the with a nice title labeling the variable and its units.
plot/along=x/L=5/x=120:200/color/thick/nokey\ /title="Sea Surface Temperature (`sst,return=units`)" sst[y=-40:0:10
Now define a viewport for the legend. In this case it overlaps the first one a bit, extending into the right-hand margin of the main plot. This is a custom plot, with all of the plot elements set up so that the legend text will fit nicely. Each case will be different, depending on the number of lines, the desired aspect ratio for the main plot, and the amount of text in the labels.
define view/x=0.85:0.98/y=0.5:0.8/axes legend set view legend ! Make a blank plot in the legend viewport, to set things up so the legend ! lines can be PLOT/OVERLAY lines plot/vs/sym=dot/nolab/noax/vlim=0:1/hlim=0:1/color=white {0,1}, {0, 1}
Or, to draw an outline around the legend box:
! plot/vs/line/nolab/noax/vlim=0:1/hlim=0:1 {0,0,1,1,0}, {0,1,1,0,0}
The legend text is going to label the latitudes. Are we running PyFerret? If so, optionally change to a font that's equal-spaced so the labels will line up, and set up to make a degree sign within the labels.
IF ($program_name"|PyFerret>1|*>0") THEN SET TEXT/FONT=consolas DEFINE SYMBOL degsgn = <°> ENDIF
Define short horizontal line segments lying in our legend plot of x=0:1, y=0:1. Draw the lines using the same line styles as on the main plot, using line number, or /COLOR=, /DASH, /SYMBOL, and so forth to match the lines drawn. Use ANNOTATE to label each line.
plot/over/vs/nolab/line=6/thick {0.2,0.5}, {0.8,0.8} annotate/norm/siz=0.14/xpos=.55/ypos=0.8/halign=-1/valign=0 "Y= 0($degsgn)S" plot/over/vs/nolab/line=5/thick {0.2,0.5}, {0.7,0.7} annotate/norm/siz=0.14/xpos=.55/ypos=0.7/halign=-1/valign=0 "Y=10($degsgn)S" plot/over/vs/nolab/line=4/thick {0.2,0.5}, {0.6,0.6} annotate/norm/siz=0.14/xpos=.55/ypos=0.6/halign=-1/valign=0 "Y=20($degsgn)S" plot/over/vs/nolab/line=3/thick {0.2,0.5}, {0.5,0.5} annotate/norm/siz=0.14/xpos=.55/ypos=0.5/halign=-1/valign=0 "Y=30($degsgn)S" plot/over/vs/nolab/line=2/thick {0.2,0.5}, {0.4,0.4} annotate/norm/siz=0.14/xpos=.55/ypos=0.4/halign=-1/valign=0 "Y=40($degsgn)S"