PyGMT colorbar length settings

Hello dear GMT community,

I will ask probably a very simple question regarding the colorbar. Nevertheless, I couldn’t come up with a solution by reading the documentation.

I want to be able to set a colorbar length to any desired percentage relative to the map side in PyGMT. In the GMT docs, it is written that the length is set to 80% of the map side, so I was wondering if there is a possibility to change that to any desired value, e.g. 100%?

Thank you.

You can use the +w modifier for position to modify the length and width of the colorbar, but it will not automatically position the colorbar.

This:

fig = pygmt.Figure()
fig.grdimage(grid=grid, frame="agf", projection="M15c")
fig.colorbar(cmap="turbo", frame="agf", position="x1/-1+w3h")
fig.show()

Produces:

It uses x to set the position of the colorbar to (1, -1), the length of the colorbar to 3 centimeters, and appends h to make the colorbar horizontal.

This:

fig = pygmt.Figure()
fig.grdimage(grid=grid, frame="agf", projection="M15c")
fig.colorbar(cmap="turbo", frame="agf", position="x1/-1+w10h")
fig.show()

Produces:

With the only difference that the colorbar length is now set to 10 centimeters, and the width is adjusted accordingly to be 4% of the length (default setting).

1 Like

Hi Igor! As far as I know there is no direct way to set the length of the scale based on the %.

You will have to:

  1. get the map height (or width) with mapproject (not sure if it is available in pygmt).
  2. Make a feature request. I think this is best. It would be good feature that I would use myself.
1 Like

Thank you @Esteban82,

No I don’t think that mapproject is available in PyGMT either. Thanks for your suggestion, I requested this feature on GitHub.

You are welcome.

Also, in the particular case you want a 100% scale, you could use the width of map.

For example, willschlitzer indicated a 15 cm map width (M15c), so the same value could be use for the colorbar position="+w15h").

This only works if you want to plot an horizontal colorbar. If you want to define the width and you want a vertical colorbar or vice versa), then you need mapproject.