Customizing Gridlines, Labels, and Scalebar in PyGMT

Hello PyGMT community,

I’ve recently had my first encounter with PyGMT and I’m excited to delve into its capabilities for crafting visualizations. Given my newcomer status, I’ve encountered a few customization challenges. As these inquiries seem straightforward, I’ve chosen to group them under a single topic to streamline the discussion.

Despite consulting the documentation, I’m unable to find the necessary details for the following:

  1. How can I adjust gridline properties, such as line(pen?) color and linewidth?

  2. I’m interested in changing the grid direction labels to localized versions. For example, I’d like to use ‘O’ (Portuguese - Oeste) instead of ‘W’ for West.

  3. I’m seeking assistance with personalizing the scalebar’s appearance. I aim to modify its height, segment count, and style. Despite my efforts, I couldn’t locate instructions on this in the documentation.

For reference, here’s the code I’m using:

import pygmt

fig = pygmt.Figure()

fig.coast(
    region=[-45, -25, -15, 0],
    projection="M6c",
    land="grey",
    water="lightblue",
    shorelines=True,
    frame=['ag']
)

fig.basemap(rose="jTR+w1.0c+lO,L,S,N+o0.2c/0.2c+f2", map_scale="jBR+w500k+o0.5c/0.5c+f+lkm") 
fig.show()

Best regards,
Robson

Hi @Robson, welcome to the forum! To quickly answer your questions:

This can be set with the MAP_GRID_PEN config. There is also MAP_GRID_PEN_PRIMARY and MAP_GRID_PEN_SECONDARY to further configure the primary and secondary grids if needed.

Pen properties follow the [width[c|i|p]],[color],[style[c|i|p]] syntax, see https://docs.generic-mapping-tools.org/latest/cookbook/features.html#specifying-pen-attributes and the line style tutorial at Line styles — PyGMT. For e.g.:

fig = pygmt.Figure()
with pygmt.config(MAP_GRID_PEN="thick,red"):
    fig.basemap(
        region=["2020-1-24T21:00", "2020-1-25T00:00", 0, 1],
        projection="X6c/2c",
        frame=["pa1Hg", "sa45mg45m", "NWse"],
    )
fig.show()

produces

This can also be set using pygmt.config, in particular, the GMT_LANGUAGE config, or the E.g.:

fig = pygmt.Figure()
with pygmt.config(GMT_LANGUAGE="PT"):
    fig.coast(
        region=[-45, -25, -15, 0],
        projection="M6c",
        land="grey",
        water="lightblue",
        shorelines=True,
        frame=["ag"],
    )
fig.show()

produces this map with Oeste instead of West.

This one is a bit trickier. You’re on the right track in using (map_scale/-L) from basemap, and for advanced configuration, you’ll need to look at the GMT documentation at basemap — GMT 6.4.0 documentation and 3. General Features — GMT 6.4.0 documentation. In particular:

  • Use MAP_SCALE_HEIGHT to configure the height
  • Segment count doesn’t seem configurable, but changing the scale bar length with +w should adjust the number of segments automatically
  • Only two styles possible - plain is the default, use +f to get the fancy scale with black/white zebra segments.

Example:

import pygmt

fig = pygmt.Figure()
with pygmt.config(MAP_SCALE_HEIGHT="20p"):
    fig.basemap(
        region=[-45, -25, -15, 0],
        projection="M6c",
        frame=True,
        map_scale="jBR+w1800k+o0.5c/0.5c+f+lkm",
    )
fig.show()

produces

Hopefully that’s enough to get you started! In general, you’ll need to learn how to jump from the PyGMT documentation to the GMT documentation for more advanced customizations (tip: look at the ‘Full option list …’ link in most PyGMT API documentation pages), but hopefully you’ll get the hang of it soon! Let us know if you have any other questions, and feel free to start another forum thread if needed.

2 Likes