Categorical cpt for plotting netCDF file with custom colours (Hex codes)

I have a NetCDF (.nc) file which contain grid-cell classified data where I have assigned values 1, 2, 3 for cells on basis of classification criteria. I plotted this using pygmt.grdimage while following basic categorical cpt provided in this palatte table link

cpt = pygmt.makecpt(cmap="categorical", series=[1,4,1], truncate=[0,3])

Is there any way I can define my own colour codes for 1, 2 and 3 valued classes using hex codes in the categorical cpt.

Hello @trivial,

welcome to the GMT forum :slightly_smiling_face:!

For a categorical colormap, you can pass one string of comma-separated colors to the cmap parameter. Use the color_model parameter to give annotations for the single colors (categories).

import pygmt

size = 5

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

# Set up categorical colormap
pygmt.makecpt(cmap="#1C1CE5,#9090E9,#0B0B7E", series=[1, 3, 1], color_model="+c1-3")

# Plot some sample data
fig.plot(x=[-2] * 3, y=[-2, 0, 2], fill=[1, 2, 3], cmap=True, style="c1c")

# Add colorbar
# Divide colorbar into equal-sized rectangles via the equalsize parameter
fig.colorbar(cmap=True, position="jMC", equalsize=0.2)

fig.show()

Thanks @yvonnefroehlich it worked