Skip to content

Commit 8540a9d

Browse files
Expand Colorbar gallery example for categorical data (#2395)
Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
1 parent 5364749 commit 8540a9d

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

examples/gallery/embellishments/colorbar.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
can be found at :gmt-docs:`cookbook/cpts.html`. You can set the ``position`` of
99
the colorbar using the following options:
1010
11-
- **j/J**: justified inside/outside the map frame using any 2-character
11+
- **j/J**: placed inside/outside the map frame using any 2-character
1212
combination of vertical (**T**\ op, **M**\ iddle, **B**\ ottom) and
1313
horizontal (**L**\ eft, **C**\ enter, **R**\ ight) alignment codes, e.g.
14-
``position="jTR"`` for top right.
14+
``position="jTR"`` for Top Right.
1515
- **g**: using map coordinates, e.g. ``position="g170/-45"`` for longitude
1616
170E, latitude 45S.
1717
- **x**: using paper coordinates, e.g. ``position="x5c/7c"`` for 5 cm, 7 cm
@@ -21,15 +21,18 @@
2121
Note that the anchor point defaults to the bottom left (**BL**). Append ``+h``
2222
to ``position`` to get a horizontal colorbar instead of a vertical one.
2323
"""
24+
2425
import pygmt
2526

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

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

35+
# ============
3336
# Create a colorbar showing the scientific rainbow - batlow
3437
fig.colorbar(
3538
cmap="batlow",
@@ -41,10 +44,11 @@
4144
scale=100,
4245
)
4346

47+
# ============
4448
# Create a colorbar suitable for surface topography - oleron
4549
fig.colorbar(
4650
cmap="oleron",
47-
# Colorbar position justified outside map frame (J) at Middle Right (MR),
51+
# Colorbar placed outside map frame (J) at Middle Right (MR),
4852
# offset (+o) by 1 cm horizontally and 0 cm vertically from anchor point,
4953
# with a length/width (+w) of 7 cm by 0.5 cm and a box for NaN values (+n)
5054
position="JMR+o1c/0c+w7c/0.5c+n+mc",
@@ -55,4 +59,25 @@
5559
scale=10,
5660
)
5761

62+
# ============
63+
# Create a colorbar suitable for categorical data - hawaii
64+
# Set up the colormap
65+
pygmt.makecpt(
66+
cmap="hawaii",
67+
series=[0, 3, 1],
68+
# Comma-separated string for the annotations of the colorbar
69+
color_model="+cA,B,C,D",
70+
)
71+
# Plot the colorbar
72+
fig.colorbar(
73+
cmap=True, # Use colormap set up above
74+
# Colorbar placed inside map frame (j) at Bottom Left (BL),
75+
# offset (+o) by 0.5 cm horizontally and 0.8 cm vertically from anchor
76+
# point, and plotted horizontally (+h)
77+
position="jBL+o0.5c/0.8c+h",
78+
box=True,
79+
# Divide colorbar into equal-sized rectangles
80+
equalsize=0.5,
81+
)
82+
5883
fig.show()

0 commit comments

Comments
 (0)