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()

pygmt.config(MAP_FRAME_TYPE=“plain”)
pygmt.config(FONT_TITLE=“20,Helvetica”)
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.colorbar(cmap=cpt,position=“JMR+o0.5c/0c+w6.9c”,L=True)
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

Blockquote

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()
pygmt.makecpt(
  cmap="rainbow",
  series="0.01/20/2+l",
  continuous=True,
  )
demo_cpt.colorbar(
  position="x0/0+w10c/.5c+h",
  cmap=True,
  frame="xa2f3g2",
  Q=True,
)
demo_cpt.show()

2 Likes

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!!