Skip to content
34 changes: 30 additions & 4 deletions examples/gallery/embellishments/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
can be found at :gmt-docs:`cookbook/cpts.html`. You can set the ``position`` of
the colorbar using the following options:
- **j/J**: justified inside/outside the map frame using any 2-character
- **j/J**: justified inside/outside the figure frame using any 2-character
combination of vertical (**T**\ op, **M**\ iddle, **B**\ ottom) and
horizontal (**L**\ eft, **C**\ enter, **R**\ ight) alignment codes, e.g.
``position="jTR"`` for top right.
Expand All @@ -21,15 +21,18 @@
Note that the anchor point defaults to the bottom left (**BL**). Append ``+h``
to ``position`` to get a horizontal colorbar instead of a vertical one.
"""

import pygmt

fig = pygmt.Figure()
fig.basemap(region=[0, 3, 6, 9], projection="x3c", frame=["af", "WSne+tColorbars"])

# ============
# Create a colorbar designed for seismic tomography - roma
# Colorbar is placed at bottom center (BC) by default if no position is given
fig.colorbar(cmap="roma", frame=["x+lVelocity", "y+lm/s"])

# ============
# Create a colorbar showing the scientific rainbow - batlow
fig.colorbar(
cmap="batlow",
Expand All @@ -41,12 +44,14 @@
scale=100,
)

# ============
# Create a colorbar suitable for surface topography - oleron
fig.colorbar(
cmap="oleron",
# Colorbar position justified outside map frame (J) at Middle Right (MR),
# offset (+o) by 1 cm horizontally and 0 cm vertically from anchor point,
# with a length/width (+w) of 7 cm by 0.5 cm and a box for NaN values (+n)
# Colorbar position justified outside figure frame (J) at Middle Right
# (MR), offset (+o) by 1 cm horizontally and 0 cm vertically from anchor
# point, with a length/width (+w) of 7 cm by 0.5 cm and a box for NaN
# values (+n)
position="JMR+o1c/0c+w7c/0.5c+n+mc",
# Note that the label 'Elevation' is moved to the opposite side and plotted
# vertically as a column of text using '+mc' in the position parameter
Expand All @@ -55,4 +60,25 @@
scale=10,
)

# ============
# Create a colorbar suitable for categorical data - hawaii
# Set up the colormap
pygmt.makecpt(
cmap="hawaii",
series=[0, 3, 1],
# Comma-separated string for the annotations of the colorbar
color_model="+cclass A,class B,class C,class D",
)
# Plot the colorbar
fig.colorbar(
cmap=True, # Use colormap set up above
# Colorbar position justified inside figure frame (j) at Bottom Left (BL),
# offset (+o) by 0.5 cm horizontally and 0.8 cm vertically from anchor
# point, and plotted horizontally (+h)
position="jBL+o0.5c/0.8c+h",
box=True,
# Divide colorbar into equal-sized rectangles
equalsize=0.5,
)

fig.show()