How to rotate colorbar labels in PyGMT?

Hi everyone! First time posting here. I’m trying to rotate the labels in the colorbar with pygmt. I tried adding -S option from GMT doc and adding +a45 to the position and frame variables in pygmt but no success.

# Add a colorbar for the elevation

fig.colorbar(
    # Place the colorbar outside the plot (upper-case "J") 
    # Move the x label above the horizontal colorbar ("+ml")
    position = ["JMR+o1c/-0.1c+w14c/0.4c+ml"],
    # Add a box around the colobar with a fill ("+g") in "white" color and
    # a transparency ("@") of 8 % 
    box="+gwhite@8",
    # Add x and y labels ("+l")
    frame=["xa2000f1000","x+lElevation", "y+lm"],
)


I would like to rotate the labels/annotations so that they are plotted parallel to the colorbar, if possible.

Hello @msc,

Welcome to the GMT forum :slightly_smiling_face:.

I just tried a simple code example. Here, appending +a to the frame parameter of Figure.colorbar works. Maybe this example can already help you to adjust your code. Otherwise please post your code, in which you try to rotate the annotations of the colorbar.

import pygmt

size = 5

fig = pygmt.Figure()
fig.basemap(region=[-size, size] * 2, projection=f"X{size*2}c", frame=0)
fig.colorbar(cmap="batlow", position="jMC+o-3c/0+ml", frame="xaf+lElevation")
fig.colorbar(cmap="batlow", position="jMC+ml", frame="xaf+a45+lElevation")
fig.colorbar(cmap="batlow", position="jMC+o3c/0c+ml", frame="xaf+a90+lElevation")
fig.show()

1 Like

Thank you so much @yvonnefroehlich! Yes, I was able to make it work by modifying the basemap projection.

If I use a geographic projection:

fig.basemap(region=[115, 119.5, 4, 7.5], projection="M15c", frame=["ag", "WsNe"] ) 

then I get this error that I was not addressing before:

colorbar [ERROR]: Option -B: Cannot use +a for geographic basemaps
colorbar [ERROR]: Option -B parsing failure. Correct syntax:
colorbar [ERROR]: Offending option -Bxaf+a90+lElevation

Yes, that’s true. Rotated anntotations are only available for Carthesian plots (4. Standardized command line options — GMT 6.6.0 documentation). And it looks like this applies not only the the frame of the figure or map, but also for the frame of the colorbar.

So far, I only have a very rude work around. There, the projection is changed to Cartesian before plotting the colorbar. Playing around with Figure.shift_origin can be also helpful.

import pygmt

fig = pygmt.Figure()

# Basemap with none Cartesian projection
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M15c", frame=True)

# !!! This is not a nice solution !!!
# !!! Be aware of that the projection changes now !!!

# First plot the map frame in red to adjust the size of the Cartesian projection
#with pygmt.config(MAP_FRAME_PEN="red"):
# In the end make the frame completely transparent (@100) to hide it
with pygmt.config(MAP_FRAME_PEN="red@100"): 
    # We need a basemap with a Cartesian projection to be allowed to rotate
    # the annotations of the colorbar
    # The size of this projection is manually adjusted based on the width and 
    # height of the projection used for the map at the beginning
    fig.basemap(region=[-1, 1] * 2, projection="X15c/11.5c", frame=0)
fig.colorbar(cmap="batlow", position="JRM+ml+w10c/0.4c", frame="xaf+a90+lElevation")

# !!! Every data you plot now is plotted with a Cartesian projection !!!

fig.show()

Yes, I also did a not so pretty work around, but this is helpful, thanks again!