# How to rotate colorbar labels in PyGMT?

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-rotate-colorbar-labels-in-pygmt/5381>\
**Category:** PyGMT Q&A\
**Created:** [September 24, 2024, 7:44pm UTC](https://forum.generic-mapping-tools.org/t/how-to-rotate-colorbar-labels-in-pygmt/5381 "2024-09-24T19:44:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![msc](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@msc](https://forum.generic-mapping-tools.org/u/msc)\
**Post date:** [September 24, 2024, 7:44pm UTC](https://forum.generic-mapping-tools.org/t/how-to-rotate-colorbar-labels-in-pygmt/5381/1 "2024-09-24T19:44:20Z")

</div>

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.

```auto
# 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"],
)

```

 ![Screenshot 2024-09-24 at 12.31.38 PM](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/2/2bf3dafe4e2907fc5d326e2a5dca02624f26b6ab.png)  
I would like to rotate the labels/annotations so that they are plotted parallel to the colorbar, if possible.

---

<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:** [September 24, 2024, 9:49pm UTC](https://forum.generic-mapping-tools.org/t/how-to-rotate-colorbar-labels-in-pygmt/5381/2 "2024-09-24T21:49:20Z")

</div>

Hello @msc,

Welcome to the GMT forum 🙂.

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.

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

```

 ![colorbar_rotate_annotations](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/3/34cc72a972f32a84c0d0a21344de8a3673106bad.png)

---

<div class="post-metadata">

**Author:** ![msc](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@msc](https://forum.generic-mapping-tools.org/u/msc)\
**Post date:** [September 25, 2024, 5:32pm UTC](https://forum.generic-mapping-tools.org/t/how-to-rotate-colorbar-labels-in-pygmt/5381/3 "2024-09-25T17:32:37Z")

</div>

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

If I use a geographic projection:

```auto
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:

```auto
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

```

---

<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:** [September 25, 2024, 6:45pm UTC](https://forum.generic-mapping-tools.org/t/how-to-rotate-colorbar-labels-in-pygmt/5381/4 "2024-09-25T18:45:52Z")

</div>

Yes, that’s true. Rotated anntotations are only available for Carthesian plots ([4. Standardized command line options — GMT 6.7.0 documentation](https://docs.generic-mapping-tools.org/dev/reference/options.html#axes-settings)). 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.

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

```

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

---

<div class="post-metadata">

**Author:** ![msc](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@msc](https://forum.generic-mapping-tools.org/u/msc)\
**Post date:** [September 25, 2024, 7:39pm UTC](https://forum.generic-mapping-tools.org/t/how-to-rotate-colorbar-labels-in-pygmt/5381/5 "2024-09-25T19:39:10Z")

</div>

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