From 8540a9dbd3cf65dedd8d75193727541f2a19f2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= <94163266+yvonnefroehlich@users.noreply.github.com> Date: Sat, 11 Mar 2023 05:03:40 +0100 Subject: [PATCH] Expand Colorbar gallery example for categorical data (#2395) Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com> --- examples/gallery/embellishments/colorbar.py | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/examples/gallery/embellishments/colorbar.py b/examples/gallery/embellishments/colorbar.py index 25844a6d97b..ebb7893a14d 100644 --- a/examples/gallery/embellishments/colorbar.py +++ b/examples/gallery/embellishments/colorbar.py @@ -8,10 +8,10 @@ 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**: placed inside/outside the map 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. + ``position="jTR"`` for Top Right. - **g**: using map coordinates, e.g. ``position="g170/-45"`` for longitude 170E, latitude 45S. - **x**: using paper coordinates, e.g. ``position="x5c/7c"`` for 5 cm, 7 cm @@ -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", @@ -41,10 +44,11 @@ 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), + # Colorbar placed 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) position="JMR+o1c/0c+w7c/0.5c+n+mc", @@ -55,4 +59,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="+cA,B,C,D", +) +# Plot the colorbar +fig.colorbar( + cmap=True, # Use colormap set up above + # Colorbar placed inside map 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()