Why pygmt.makecpt does not work when it is used multiple times

Problem

pygmt.makecpt works the first time, but on subsequent attempts, it is ignored and the first-generated CPT is used.

I have found I can use makecpt multiple times by reloading pygmt,
but I do not think it is natural way.

Question

  • In the example below why is the second makecpt ignored?
  • Are there any other ways to use makecpt multiple times?

Example

  • run on jupyter
  • pygmt version: 0.14.0
import importlib
import pygmt

rgn = [90, 180, 0, 60]
prj = "M5c"
grd = pygmt.datasets.load_earth_relief(
    resolution="15m",
    region = rgn,
    )

importlib.reload(pygmt)

with pygmt.config(
    MAP_TITLE_OFFSET = "-5p",
    FONT_LABEL = "1p",
    FONT_TITLE = "10p",
):
    # =======================
    # fig. 1
    # =======================
    pygmt.makecpt(
        cmap="relief",
        series = [-8000,8000],
        )
    fig = pygmt.Figure()
    fig.grdimage(
        grid = grd,
        projection = prj,
        region = rgn,
        frame = [
            "af",
            "+tFig. 1",
        ],
    )
    fig.show()
    
    # =======================
    # fig. 2
    # =======================
    # reload
    # importlib.reload(pygmt)
    
    pygmt.makecpt(
        cmap="geo",
        series = [-8000,8000],
        )
    fig = pygmt.Figure()
    fig.grdimage(
        grid = grd,
        projection = prj,
        region = rgn,
        frame = [
            "af",
            "+tFig. 2",
        ],
    )
    fig.show()

Output

I tried to generate two colormaps from relief and geo,
but the same cmap derives from relief is used for both fig. 1 and fig. 2.
If I reload pygmt before the second makecpt, it works correctly.

In this case, I know I can change cmap by giving cmap in grdimage.
Here, the problem is I can not make multiple cmaps in one session without reloading pygmt.

Hey @kei,

Welcome to the GMT forum :slightly_smiling_face:!

You need to setup your colormap (pygmt.makecpt) after setting up the Figure instance (pygmt.Figure()):

import pygmt

rgn = [90, 180, 0, 60]
prj = "M5c"
grd = pygmt.datasets.load_earth_relief(resolution="01d",region=rgn)

# =======================
# fig. 1
# =======================
fig = pygmt.Figure()
pygmt.makecpt(cmap="relief", series=[-8000, 8000])
fig.grdimage(grid=grd, projection=prj, region=rgn, frame=["af", "+tFig. 1"])
fig.show()

# =======================
# fig. 2
# =======================
fig = pygmt.Figure()
pygmt.makecpt(cmap="oleron", series=[-8000, 8000])
fig.grdimage(grid=grd, projection=prj, region=rgn, frame=["af", "+tFig. 2"])
fig.show()

Hi @yvonnefroehlich .

Oh, it was a simple mistake.
Thank you for your help! :partying_face: