Skip to content

Commit 7554e3b

Browse files
seismanyvonnefroehlichmichaelgrund
authored
DOC: Update Figure.colorbar to the new syntax in tutorials/examples (#4328)
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
1 parent f78251d commit 7554e3b

12 files changed

Lines changed: 117 additions & 64 deletions

File tree

doc/techref/justification_codes.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,14 @@ Script showing justification codes for plot embellishments, e.g., a colorbar.
105105
fig = pygmt.Figure()
106106
fig.basemap(projection="X10c/2c", region=[-size, size, -size, size], frame=0)
107107
108-
fig.colorbar(cmap="SCM/buda", frame=0, position="jMC+w10c/2c+h")
108+
fig.colorbar(
109+
cmap="SCM/buda",
110+
frame=0,
111+
position="MC",
112+
length=10,
113+
width=2,
114+
orientation="horizontal",
115+
)
109116
110117
for code in codes:
111118
fig.text(

examples/gallery/3d_plots/grdview_surface.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919
import pygmt
2020
import xarray as xr
21+
from pygmt.params import Position
2122

2223

2324
# Define an interesting function of two variables, see:
@@ -46,8 +47,7 @@ def ackley(x, y):
4647
SCALE = 0.5 # in centimeters
4748
fig.grdview(
4849
data,
49-
# Set annotations and gridlines in steps of five, and
50-
# tick marks in steps of one
50+
# Set annotations and gridlines in steps of five, and tick marks in steps of one
5151
frame=["a5f1g5", "za5f1g5"],
5252
projection=f"x{SCALE}c",
5353
zscale=f"{SCALE}c",
@@ -57,10 +57,7 @@ def ackley(x, y):
5757
shading="+a45",
5858
)
5959

60-
# Add colorbar for gridded data
61-
fig.colorbar(
62-
frame="a2f1", # Set annotations in steps of two, tick marks in steps of one
63-
position="JMR", # Place colorbar in the Middle Right (MR) corner
64-
)
60+
# Add colorbar for gridded data in the Middle Right corner.
61+
fig.colorbar(frame="a2f1", position=Position("MR", cstype="outside"))
6562

6663
fig.show()

examples/gallery/basemaps/ternary.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# %%
1616
import pygmt
17+
from pygmt.params import Position
1718

1819
fig = pygmt.Figure()
1920

@@ -40,7 +41,8 @@
4041
],
4142
)
4243

43-
# Add a colorbar indicating the values given in the fourth column of
44-
# the input dataset
45-
fig.colorbar(position="JBC+o0c/1.5c", frame="x+lPermittivity")
44+
# Add a colorbar indicating the values given in the fourth column of the input dataset
45+
fig.colorbar(
46+
position=Position("BC", cstype="outside", offset=(0, 1.5)), frame="x+lPermittivity"
47+
)
4648
fig.show()

examples/gallery/embellishments/colorbar.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,18 @@
22
Colorbar
33
========
44
5-
The :meth:`pygmt.Figure.colorbar` method creates a color scalebar.
6-
The colormap is set via the ``cmap`` parameter. A full list of available
7-
color palette tables can be found at :gmt-docs:`reference/cpts.html`.
8-
Use the ``frame`` parameter to add labels to the **x** and **y** axes
9-
of the colorbar by appending **+l** followed by the desired text. To add
10-
and adjust the annotations (**a**) and ticks (**f**) append the letter
11-
followed by the desired interval. The placement of the colorbar is set
12-
via the ``position`` parameter. There are the following options:
13-
14-
- **j/J**: placed inside/outside the plot bounding box using a
15-
:doc:`2-character justification code </techref/justification_codes>`, e.g.,
16-
``position="jTR"`` for Top Right.
17-
- **g**: using map coordinates, e.g. ``position="g170/-45"`` for longitude
18-
170° East, latitude 45° South.
19-
- **x**: using paper coordinates, e.g. ``position="x5c/7c"`` for 5 cm, 7 cm
20-
from anchor point.
21-
- **n**: using normalized (0-1) coordinates, e.g. ``position="n0.4/0.8"``.
22-
23-
Note that the anchor point defaults to Bottom Left (**BL**). Append ``+h`` to
24-
``position`` to get a horizontal colorbar instead of a vertical one (``+v``).
5+
The :meth:`pygmt.Figure.colorbar` method creates a color scalebar. The colormap is set
6+
via the ``cmap`` parameter. A full list of available color palette tables can be found
7+
at :gmt-docs:`reference/cpts.html`. Use the ``frame`` parameter to add labels to the
8+
**x** and **y** axes of the colorbar by appending **+l** followed by the desired text.
9+
To add and adjust the annotations (**a**) and ticks (**f**) append the letter followed
10+
by the desired interval. The placement of the colorbar is set by passing a
11+
:class:`pygmt.params.Position` object to the ``position`` parameter.
2512
"""
2613

2714
# %%
2815
import pygmt
16+
from pygmt.params import Position
2917

3018
fig = pygmt.Figure()
3119
fig.basemap(region=[0, 3, 6, 9], projection="x3c", frame=["af", "WSne+tColorbars"])
@@ -41,9 +29,12 @@
4129
# Create a colorbar showing the scientific rainbow - batlow
4230
fig.colorbar(
4331
cmap="SCM/batlow",
44-
# Colorbar positioned at map coordinates (g) longitude/latitude 0.3/8.7,
45-
# with a length/width (+w) of 4 cm by 0.5 cm, and plotted horizontally (+h)
46-
position="g0.3/8.7+w4c/0.5c+h",
32+
# A horizontal colorbar positioned at map coordinates (0.3, 8.7), with a
33+
# length of 4 cm and a width of 0.5 cm.
34+
position=Position((0.3, 8.7), cstype="mapcoords"),
35+
length=4,
36+
width=0.5,
37+
orientation="horizontal",
4738
box=True,
4839
frame=["x+lTemperature", "y+l°C"],
4940
scale=100,
@@ -53,13 +44,17 @@
5344
# Create a colorbar suitable for surface topography - oleron
5445
fig.colorbar(
5546
cmap="SCM/oleron",
56-
# Colorbar placed outside the plot bounding box (J) at Middle Right (MR),
57-
# offset (+o) by 1 cm horizontally and 0 cm vertically from anchor point,
58-
# with a length/width (+w) of 7 cm by 0.5 cm and a box for NaN values (+n)
59-
position="JMR+o1c/0c+w7c/0.5c+n+mc",
47+
# Colorbar placed at Middle Right (MR) outside the plot bounding box, offset by 1 cm
48+
# horizontally and 0 cm vertically from anchor point, with a length of 7 cm and
49+
# width of 0.5 cm, and a rectangle for NaN values.
6050
# Note that the label 'Elevation' is moved to the opposite side and plotted
61-
# vertically as a column of text using '+mc' in the position parameter
62-
# above
51+
# vertically as a column of characters.
52+
position=Position("MR", cstype="outside", offset=(1, 0)),
53+
length=7,
54+
width=0.5,
55+
nan=True,
56+
move_text=["annotations", "label"],
57+
label_as_column=True,
6358
frame=["x+lElevation", "y+lm"],
6459
scale=10,
6560
)
@@ -76,10 +71,11 @@
7671
# Plot the colorbar
7772
fig.colorbar(
7873
cmap=True, # Use colormap set up above
79-
# Colorbar placed inside the plot bounding box (j) in the Bottom Left (BL) corner,
80-
# with an offset (+o) by 0.5 cm horizontally and 0.8 cm vertically from the anchor
81-
# point, and plotted horizontally (+h)
82-
position="jBL+o0.5c/0.8c+h",
74+
# Colorbar placed in the Bottom Left (BL) corner inside the plot bounding box, with
75+
# an offset by 0.5 cm horizontally and 0.8 cm vertically from the anchor point, and
76+
# plotted horizontally.
77+
position=Position("BL", offset=(0.5, 0.8)),
78+
orientation="horizontal",
8379
box=True,
8480
# Divide colorbar into equal-sized rectangles
8581
equalsize=0.5,

examples/gallery/images/cross_section.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# %%
1616
import pygmt
17-
from pygmt.params import Box
17+
from pygmt.params import Box, Position
1818

1919
# Define region of study area
2020
# lon_min, lon_max, lat_min, lat_max in degrees East and North
@@ -41,10 +41,14 @@
4141

4242
# Add a colorbar for the elevation
4343
fig.colorbar(
44-
# Place the colorbar inside the plot (lowercase "j") in the Bottom Right (BR)
45-
# corner with an offset ("+o") of 0.7 centimeters and 0.3 centimeters in x- or y-
46-
# directions, respectively; move the x-label above the horizontal colorbar ("+ml")
47-
position="jBR+o0.7c/0.8c+h+w5c/0.3c+ml",
44+
# Place the colorbar inside the plot in the Bottom Right (BR) corner with an offset
45+
# of 0.7 centimeters and 0.3 centimeters in x- or y-directions, respectively;
46+
# move the x-label above the horizontal colorbar.
47+
position=Position("BR", offset=(0.7, 0.8)),
48+
length=5,
49+
width=0.3,
50+
orientation="horizontal",
51+
move_text="label",
4852
# Add a box around the colobar, filled in white and a 30 % transparency, with a
4953
# 0.8-point thick, black, outline.
5054
box=Box(pen="0.8p,black", fill="white@30"),

examples/gallery/images/grdclip.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
Clipping grid values
33
====================
44
5-
The :func:`pygmt.grdclip` function allows to clip defined ranges of grid
6-
values. In the example shown below we set all elevation values (grid points)
7-
smaller than 0 m (in general the bathymetric part of the grid) to a common
8-
value of -2000 m via the ``below`` parameter.
5+
The :func:`pygmt.grdclip` function allows to clip defined ranges of grid values. In the
6+
example shown below we set all elevation values (grid points) smaller than 0 m (in
7+
general the bathymetric part of the grid) to a common value of -2000 m via the ``below``
8+
parameter.
99
"""
1010

1111
# %%
1212
import pygmt
13+
from pygmt.params.position import Position
1314

1415
fig = pygmt.Figure()
1516

@@ -41,6 +42,10 @@
4142
frame=["wSne+tclipped grid", "xa5f1", "ya2f1"],
4243
)
4344
fig.grdimage(grid=grid)
44-
fig.colorbar(frame=["x+lElevation", "y+lm"], position="JMR+o0.5c/0c+w8c")
45+
fig.colorbar(
46+
frame=["x+lElevation", "y+lm"],
47+
position=Position("MR", cstype="outside", offset=(0.5, 0)),
48+
length=8,
49+
)
4550

4651
fig.show()

examples/gallery/images/grdgradient.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# %%
1515
import pygmt
16+
from pygmt.params import Position
1617

1718
# Define region of interest around Yosemite valley
1819
region = [-119.825, -119.4, 37.6, 37.825]
@@ -37,8 +38,12 @@
3738
frame=["WSrt+tOriginal Data Elevation Model", "xa0.1", "ya0.1"],
3839
cmap=True,
3940
)
40-
41-
fig.colorbar(position="JML+o1.4c/0c+w7c/0.5c", frame=["xa1000f500+lElevation", "y+lm"])
41+
fig.colorbar(
42+
position=Position("ML", cstype="outside", offset=(1.4, 0)),
43+
length=7,
44+
width=0.5,
45+
frame=["xa1000f500+lElevation", "y+lm"],
46+
)
4247

4348
# --------------- plotting the hillshade map -----------
4449

examples/gallery/images/grdgradient_shading.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
# %%
2222
import pygmt
23+
from pygmt.params import Position
2324

2425
# Load the 3 arc-minutes global relief grid in the target area around Caucasus
2526
grid = pygmt.datasets.load_earth_relief(resolution="03m", region=[35, 50, 35, 45])
@@ -59,6 +60,12 @@
5960
panel=True,
6061
)
6162

62-
fig.colorbar(position="JBC+w10c/0.25c+h", frame="xa2000f500+lElevation (m)")
63+
fig.colorbar(
64+
position=Position("BC", cstype="outside"),
65+
length=14,
66+
width=0.4,
67+
orientation="horizontal",
68+
frame="xa2000f500+lElevation (m)",
69+
)
6370

6471
fig.show()

examples/gallery/images/grdlandmask.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# %%
1010
import pygmt
11+
from pygmt.params import Position
1112

1213
fig = pygmt.Figure()
1314

@@ -33,6 +34,6 @@
3334
pygmt.makecpt(cmap="SCM/batlow", series=(0, 1, 1), color_model="+cwater,land")
3435

3536
fig.grdimage(grid=grid, cmap=True)
36-
fig.colorbar(position="JMR+o0.5c/0c+w8c")
37+
fig.colorbar(position=Position("MR", cstype="outside", offset=(0.5, 0)), length=8)
3738

3839
fig.show()

examples/gallery/maps/choropleth_map.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# %%
1717
import geopandas as gpd
1818
import pygmt
19+
from pygmt.params.position import Position
1920

2021
provider = "https://naciscdn.org/naturalearth"
2122
world = gpd.read_file(f"{provider}/110m/cultural/ne_110m_admin_0_countries.zip")
@@ -36,6 +37,13 @@
3637
fig.plot(data=africa, pen="0.8p,gray50", fill="+z", cmap=True, aspatial="Z=POP_EST")
3738

3839
# Add colorbar legend.
39-
fig.colorbar(frame="x+lPopulation (millions)", position="jML+o2c/-2.5c+w5c+ef0.2c+ml")
40+
fig.colorbar(
41+
frame="x+lPopulation (millions)",
42+
position=Position("ML", offset=(2, -2.5)),
43+
length=5,
44+
fg_triangle=True,
45+
triangle_height=0.2,
46+
move_text="label",
47+
)
4048

4149
fig.show()

0 commit comments

Comments
 (0)