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

**URL:** <https://forum.generic-mapping-tools.org/t/can-we-use-cmap-rainbow-to-create-non-equidistant-color-legends/3570>\
**Category:** Q&A\
**Created:** [December 27, 2022, 8:24am UTC](https://forum.generic-mapping-tools.org/t/can-we-use-cmap-rainbow-to-create-non-equidistant-color-legends/3570 "2022-12-27T08:24:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![10Kang](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/10kang/32/2553_2.png) [@10Kang](https://forum.generic-mapping-tools.org/u/10Kang)\
**Post date:** [December 27, 2022, 8:24am UTC](https://forum.generic-mapping-tools.org/t/can-we-use-cmap-rainbow-to-create-non-equidistant-color-legends/3570/1 "2022-12-27T08:24:10Z")

</div>

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.

 ![jj](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/8/87727c5fe8474c546b7361dc806a911f38771f77.jpeg)

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](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/4/487c1d0b9467c18da0ca8436c9cab5c7613c2a61.png)

> Blockquote

---

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [December 27, 2022, 5:54pm UTC](https://forum.generic-mapping-tools.org/t/can-we-use-cmap-rainbow-to-create-non-equidistant-color-legends/3570/2 "2022-12-27T17:54:03Z")

</div>

Hello @10Kang - welcome to the GMT community!

Maybe [`grd2cpt`](https://docs.generic-mapping-tools.org/latest/grd2cpt.html) 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](https://docs.generic-mapping-tools.org/latest/grd2cpt.html), 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.

---

<div class="post-metadata">

**Author:** ![jaltekruse](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jaltekruse/32/1607_2.png) [@jaltekruse](https://forum.generic-mapping-tools.org/u/jaltekruse)\
**Post date:** [December 27, 2022, 11:22pm UTC](https://forum.generic-mapping-tools.org/t/can-we-use-cmap-rainbow-to-create-non-equidistant-color-legends/3570/3 "2022-12-27T23:22:30Z")

</div>

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](https://flint.soest.hawaii.edu/t/more-colorbar-user-errors-or-bugs/1983).

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

```

 ![rainbow_log_cpt](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/b/b4fa5d4de55f25528aaf3bd833e51169823d64ae.png)

---

<div class="post-metadata">

**Author:** ![10Kang](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/10kang/32/2553_2.png) [@10Kang](https://forum.generic-mapping-tools.org/u/10Kang)\
**Post date:** [December 28, 2022, 2:36am UTC](https://forum.generic-mapping-tools.org/t/can-we-use-cmap-rainbow-to-create-non-equidistant-color-legends/3570/4 "2022-12-28T02:36:49Z")

</div>

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