pyGMT makecpt - background and foreground

Hi, is there an equivalent makecpt -D option in pygmt for setting the background and foreground colours for the lowest and highest z-values? I couldn’t see any obvious usage information for this in pygmt.makecpt.
(Apologies in advance if this is the wrong place to post pygmt questions).

So far I used the M flag with an empty string which ensures that your fore and background colors defined e.g. via

pygmt.config(COLOR_BACKGROUND = 'white',
         COLOR_FOREGROUND = 'black')

are used:

pygmt.makecpt(cmap = 'viridis', reverse = True, series = [0, 180, 10], output = outcpt.cpt', continuous = True, M = '')

Hope that helps. I assume that in future the M flag may also be accessible via another alias.

Hi @shicks-seismo, yes, this is the place to ask questions! Sorry for the poor documentation at pygmt.makecpt, you should be able to pass the D argument directly into makecpt. This applies to any single character arguments you find in GMT’s makecpt, even though it would be nice to use a proper alias for better readability (it’s a work in progress).

Just building on from @mgrund’s post, here’s an example of how to set the background/foreground colour. The following code turns areas below sea level (0m) to lightblue and areas above 3000m to red:

import pygmt

grid = pygmt.datasets.load_earth_relief()

fig = pygmt.Figure()
with pygmt.config(COLOR_FOREGROUND="red", COLOR_BACKGROUND="lightblue"):
    pygmt.makecpt(
        cmap="batlow", series=[0, 3000, 10], continuous=True, M=True,
    )
fig.grdimage(grid=grid)
fig.savefig(fname="makecpt_bg_fg.png")
fig.show()

produces:

The -D argument would only set the background/foreground colour according to what’s found in the CPT. In the example above using ‘batlow’, that would be a darkblue/brightpink sort of background/foreground colour which you can’t change. Using -M allows you to set those background/foreground colours manually.

Note that if you combine -D with -M, only COLOR_NAN is considered (useful for colouring blank pixels). My friend has a good (pure GMT) tutorial you can refer to at https://gmt-tutorials.org/en/cpt_colormap.html which shows what this looks like.

1 Like