pygmt.Figure.legend

Figure.legend(spec=None, position=None, width=None, height=None, line_spacing=None, box=False, projection=None, region=None, verbose=False, panel=False, transparency=None, perspective=False, **kwargs)

Plot a legend.

Makes legends that can be overlaid on plots. It reads specific legend-related information from an input file, a io.StringIO object, or automatically creates legend entries from plotted symbols that have labels. Unless otherwise noted, annotations will be made using the primary annotation font and size in effect (i.e., FONT_ANNOT_PRIMARY).

Full GMT docs at https://docs.generic-mapping-tools.org/6.6/legend.html.

Aliases:

  • D = position, +w: width/height, +l: line_spacing

  • F = box

  • J = projection

  • R = region

  • V = verbose

  • c = panel

  • p = perspective

  • t = transparency

Parameters:
  • spec (str | PathLike | StringIO | None, default: None) –

    The legend specification. It can be:

    • None which means using the automatically generated legend specification file

    • Path to the legend specification file

    • A io.StringIO object containing the legend specification

    See https://docs.generic-mapping-tools.org/6.6/legend.html for the definition of the legend specification.

  • position (Position | None, default: None) – Specify the position of the legend on the plot. If not specified, defaults to the top right corner inside the plot with a 0.2-cm offset. See pygmt.enums.Position for details.

  • width (float | str | None, default: None)

  • height (float | str | None, default: None) –

    Specify the width and height of the legend box in plot coordinates (inches, cm, etc.). If not given, the width and height are computed automatically based on the contents of the legend specification.

    If unit is % (percentage) then width is computed as that fraction of the plot width. If height is given as percentage then height is recomputed as that fraction of the legend width (not plot height).

    Note: Currently, the automatic height calculation only works when legend codes D, H, L, S, or V are used and that the number of symbol columns (N) is 1.

  • line_spacing (float | None, default: None) – Specify the line-spacing factor between legend entries in units of the current font size [Default is 1.1].

  • box (Box | bool, default: False) – Draw a background box behind the legend. If set to True, a simple rectangular box is drawn using MAP_FRAME_PEN. To customize the box appearance, pass a pygmt.params.Box object to control style, fill, pen, and other box properties.

  • projection (str | None, default: None) – projcode[projparams/]width|scale. Select map projection.

  • region (str or list) – xmin/xmax/ymin/ymax[+r][+uunit]. Specify the region of interest.

  • verbose (bool or str) – Select verbosity level [Full usage].

  • panel (int | Sequence[int] | bool, default: False) –

    Select a specific subplot panel. Only allowed when used in Figure.subplot mode.

    • True to advance to the next panel in the selected order.

    • index to specify the index of the desired panel.

    • (row, col) to specify the row and column of the desired panel.

    The panel order is determined by the Figure.subplot method. row, col and index all start at 0.

  • perspective (float | Sequence[float] | str | bool, default: False) –

    Select perspective view and set the azimuth and elevation of the viewpoint.

    Accepts a single value or a sequence of two or three values: azimuth, (azimuth, elevation), or (azimuth, elevation, zlevel).

    • azimuth: Azimuth angle of the viewpoint in degrees [Default is 180, i.e., looking from south to north].

    • elevation: Elevation angle of the viewpoint above the horizon [Default is 90, i.e., looking straight down at nadir].

    • zlevel: Z-level at which 2-D elements (e.g., the map frame) are drawn. Only applied when used together with zsize or zscale. [Default is at the bottom of the z-axis].

    Alternatively, set perspective=True to reuse the perspective setting from the previous plotting method, or pass a string following the full GMT syntax for finer control (e.g., adding +w or +v modifiers to select an axis location other than the plot origin). See https://docs.generic-mapping-tools.org/6.6/gmt.html#perspective-full for details.

  • transparency (float) – Set transparency level, in [0-100] percent range [Default is 0, i.e., opaque]. Only visible when PDF or raster format output is selected. Only the PNG format selection adds a transparency layer in the image (for further processing).

Examples using pygmt.Figure.legend

Double Y-axes graph

Double Y-axes graph

Legend

Legend

Horizontal and vertical lines

Horizontal and vertical lines

Scatter plots with a legend

Scatter plots with a legend

3. Figure elements

3. Figure elements

Cartesian histograms

Cartesian histograms

Creating legends

Creating legends

Plotting data points

Plotting data points