# Customized annotation for colorbar - PyGMT

**URL:** <https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892>\
**Category:** PyGMT Q&A\
**Created:** [April 27, 2023, 1:12am UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892 "2023-04-27T01:12:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfpoveda](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/sfpoveda/32/3066_2.png) [@sfpoveda](https://forum.generic-mapping-tools.org/u/sfpoveda)\
**Post date:** [April 27, 2023, 1:12am UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892/1 "2023-04-27T01:12:12Z")

</div>

Hi,

I’ve tried to make a colorbar with customized annotations (irregular intervals) in PyGMT, but all attempts failed. I want to generate a colorbar like this:

 ![image](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/7/7c89fffdfebc336b36fea3b3d93936c737c79820.png)  
But I do not know how to modify the code so that I get those results.  
This is my code. I’d appreciate any help!

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

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [April 27, 2023, 1:31am UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892/2 "2023-04-27T01:31:51Z")

</div>

Does changing your `f'a{100}'` to `"a"` works?

---

<div class="post-metadata">

**Author:** ![sfpoveda](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/sfpoveda/32/3066_2.png) [@sfpoveda](https://forum.generic-mapping-tools.org/u/sfpoveda)\
**Post date:** [April 27, 2023, 1:46am UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892/3 "2023-04-27T01:46:59Z")

</div>

Yep, f’a{100}’ works the same as a100. But I do not think this is the problem. The real problem is that PyGMT only allows me to plot a regular interval in the annotations (for the case of a100 that would be 0, 100, 200, 300, …, 800) and instead I need an irregular interval (0, 30, 70, 300, 500, 800). When trying with only “a” instead of “a100”, a regular interval continues appearing

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [April 27, 2023, 9:41am UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892/4 "2023-04-27T09:41:29Z")

</div>

Hello @sfpoveda,

welcome to the GMT forum!  
So far, I can only provide a workaround to accomplish colorbar annotations based on the (irregular) values in a personal colormap. Calling the `pygmt.Figure.colorbar` method twice may work here, even it is not elegant or really satisfying.

_Code example_:

```auto
import pygmt

fig = pygmt.Figure()

# Set up a personal (un-equal) colormap
pygmt.makecpt(
    cmap="red,yellow,green,purple,blue",
    series="0,30,70,300,500,800",
    reverse=False,
)

fig.basemap(
    region=[0, 10, 0, 3],
    projection="X10c/3c",
    frame=True,
)

# Workaround: Call colorbar method twice
# First
# Add x label via the frame parameter
fig.colorbar(
    # Annotation (a) step must be larger than highest value in the colormap,
    # as then no annotations are visible
    # Set tick (f) step to 100
    # Add label (+l) 
    frame=["xa801f100+lProfundidad in km"],
)
# Second
# Use defaults of frame parameter
# Then the annotations match the intervals of the personal colormap
fig.colorbar()

fig.show()
# fig.savefig(fname="unequal_cb.png")

```

_Output figure_:

 ![unequal_cb](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/5/56996436bfea453907c552fda26037a85591f4a5.png)

* * *

For next time posting, please provide your code not in form of a screen shot, but just copy and past it. Then people can simply cope it, which makes it easier to help you 🙂. You can format your script as code by placing three backticks in the line before and after the block with the script:

````auto
```
your script formated as code
```

````

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [April 27, 2023, 10:05am UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892/5 "2023-04-27T10:05:12Z")

</div>

It seems it’s impossible to use the default annotations but also add a label to a colorbar.

Here is a minimal bash script to reproduce it:

```auto
gmt begin map
gmt makecpt -Cred,yellow,green,purple,blue -T0,30,70,300,500,800
gmt colorbar -Dx0c/0c+w10c+h -B+l"X label"
gmt end show

```

Ping @pwessel

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [April 27, 2023, 10:35am UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892/6 "2023-04-27T10:35:56Z")

</div>

Seems true. I recommend using the y-label to set units or whatever should apply to the cpt axis.

---

<div class="post-metadata">

**Author:** ![sfpoveda](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/sfpoveda/32/3066_2.png) [@sfpoveda](https://forum.generic-mapping-tools.org/u/sfpoveda)\
**Post date:** [April 27, 2023, 3:37pm UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892/7 "2023-04-27T15:37:30Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [April 28, 2023, 12:38pm UTC](https://forum.generic-mapping-tools.org/t/customized-annotation-for-colorbar-pygmt/3892/8 "2023-04-28T12:38:35Z")

</div>

Thanks! For units and not too long labels this is a good idea.
