Can we use cmap 'rainbow' to create non-equidistant color legends

I am new to PyGMT, and i encounter a problem. Is there any possible way for us to use cmap ‘rainbow’ to create an equidistant color legends with series=[‘0.01,0.03,0.05,0.1,0.2,0.3,0.5,1,3,5,10,20’] which are not equally spaced.

This is how the code look like:

fig = pygmt.Figure()

cpt = pygmt.makecpt(rainbow=‘rainbow’,series=[‘0.01,0.03,0.05,0.1,0.2,0.3,0.5,1,3,5,10,20’],continuous=True)
fig.grdimage(grid=chla_oct_2019.CHL[0], cmap=cpt, projection=“M15c”, frame [“WSne”,“xa10f5”,“yaf5”])
fig.plot(data=np.array([[76.5,-2,95,2]]), style=‘r+s’, pen=“1p,white”)

Any way of making it into something like this.Screenshot 2022-12-27 at 4.32.35 PM


Hello @10Kang - welcome to the GMT community!

Maybe grd2cpt solves your problem?

grd2cpt reads one or more grid files and writes a static color palette (CPT) file. […] The CPT is based on an existing dynamic master CPT of your choice, and the mapping from data value to colors is through the data’s cumulative distribution function (CDF), so that the colors are histogram equalized. Thus if the grid(s) and the resulting CPT are used in grdimage with a linear projection, the colors will be uniformly distributed in area on the plot.

Quote from the grd2cpt docs, emphasis mine.

It is not exactly what you are asking for but maybe a starting point. I understand you want to show more details in the lower range of your data so maybe this works for you.

1 Like

See the notes on generating a 1D array for the series argument to makecpt, here I use 0.01/20/2+l for log10 of min and max, for 1, 2, and 5 times magnitude. Then plot the color bar using a log scale, Q=True. I can’t seem to get gridlines on the color bar (the g2 is ignored).

This doesn’t generate your exact tick marks, but I tried to do something similar in this post.

demo_cpt = pygmt.Figure()


Thank you so much for these kind assistance, I have figured it out doing that using this way.

cpt = pygmt.makecpt(cmap=“rainbow”,series=‘0.01/10/1’,log=‘o’,continuous=True)
fig.colorbar(cmap=cpt,position=“JMR+o0.2c/0c+w6.4c”,frame=[‘xaf’,‘x+lChl (mg/m@+3@+)’],Q=True)

Thank you so much!!