Pygmt.makecpt

hello, I am very confused, I wang to create cpt by rgb codes, but it isn`t workable

here is my code
pygmt.makecpt(cmap=“batlow”, series=(minLevel, maxLevel, 1),
color_model=“r288/150/53, 0/0/0, 196/40/27, 165/155/143, 168/235/255, 97/97/97, 227/226/195”,
output=r"D:\data\red.cpt")

the output
5 14.25/55.25/94.25 6 14.25/55.25/94.25 L
6 33.75/95.75/97 7 33.75/95.75/97 L
7 93.5/120/67.75 8 93.5/120/67.75 L
8 170.5/140/44.75 9 170.5/140/44.75 L
9 241.25/157/107.5 10 241.25/157/107.5 L
10 253/187.75/202.5 11 253/187.75/202.5 B
B 1/25/89
F 250/204/250
N 255/255/255

Hello @kjz1997,

if you want to create a colormap from specific colors (for example given as RGB codes) you have to pass them as a comma-separated string (without with with spaces) to the cmap parameter. The color_model parameter can be used to write a colormap in RGB codes to a file via the output parameter.

import pygmt

size = 5

pygmt.makecpt(
    cmap="88/150/53,0/0/0,196/40/27,165/155/143,168/235/255,97/97/97,227/226/195",
    series=[0, 7, 1],
)

fig = pygmt.Figure()
fig.basemap(region=[-size, size, -size, size], projection=f"X{size}c", frame=0)

fig.colorbar(position="jMC")

fig.show()

1 Like

Hi @kjz1997,

As @yvonnefroehlich demonstrated, the code works well. If you want better control over the colors, this is a typical problem where ChatGPT or Gemini can assist. For example, I copied @yvonnefroehlich’s code and pasted it into the prompt “Suggest changes to the code below, expanding to 8 strong colors ranging from blue to red, without including black or white,” and the result is:

import pygmt

size = 5

# Define the colormap with 8 strong colors from blue to red
colors = "0/0/255,0/128/255,0/255/255,0/255/0,255/255/0,255/128/0,255/0/0,128/0/0"

pygmt.makecpt(
    cmap=colors,
    series=[0, 7, 1],
)

fig = pygmt.Figure()
fig.basemap(region=[-size, size, -size, size], projection=f"X{size}c", frame=0)

fig.colorbar(position="jMC")

fig.show()

Somtimes, you just need adapt your code using the power of AI.

Well, thank you ,that is a very good suggestion