Customized annotation for colorbar - PyGMT

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:


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!

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

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

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:

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:


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 :slightly_smiling_face:. You can format your script as code by placing three backticks in the line before and after the block with the script:

```
your script formated as code
```
1 Like

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:

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

1 Like

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

1 Like

Thanks!

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