Projection Module — Static Projected ("Globe") Map Frames#
The cleopatra.projection module adds a single, stateless helper:
apply_projection_frame turns a plain matplotlib Axes into a static projected
("globe") frame. It sets the projected limits and equal aspect, draws the projection
boundary (the globe's circle, Robinson's rounded rectangle, ...), optionally clips
the existing data layers to that boundary, and draws the graticule polylines.
The module is pure matplotlib with no PROJ/CRS dependency. It only receives already-computed geometry — boundary vertices, graticule polylines, and projected limits — as plain arrays. Whatever produces the projection (reprojecting data and deriving the boundary/graticule) lives upstream; cleopatra only renders the result. This keeps the engine split clean: the upstream owns CRS/PROJ, cleopatra owns matplotlib.
Usage#
import matplotlib
matplotlib.use("Agg") # any backend; Agg shown for headless rendering
import numpy as np
import matplotlib.pyplot as plt
from cleopatra.projection import apply_projection_frame
# Geometry comes in as plain arrays (here a unit-circle globe outline and a
# meridian); upstream produces the real projected boundary/graticule.
theta = np.linspace(0, 2 * np.pi, 200)
boundary = np.column_stack([np.cos(theta), np.sin(theta)])
meridian = np.column_stack([np.zeros(50), np.linspace(-1, 1, 50)])
fig, ax = plt.subplots()
# plot your (already reprojected) data first so it can be clipped ...
ax.imshow(np.zeros((8, 8)), extent=(-1, 1, -1, 1))
# ... then frame the axes as a globe and clip the data to the boundary
patch = apply_projection_frame(
ax,
boundary_xy=boundary,
xlim=(-1, 1),
ylim=(-1, 1),
graticule_lines=[meridian],
)
fig.savefig("globe.png")
Note
apply_projection_frame performs no reprojection — pass already-densified,
already-projected geometry as (N, 2) arrays. It is a one-shot helper: each call
appends a fresh boundary patch and graticule lines, so apply it once per axes (create a
new axes to re-frame). With clip_artists=True (default) every existing
ax.images/ax.collections/ax.lines artist — and the graticule — is clipped to the
boundary; pass clip_artists=False to leave the data layers unclipped. Style the
boundary and graticule via boundary_kw / graticule_kw, which override
DEFAULT_BOUNDARY_KW / DEFAULT_GRATICULE_KW.
Module Documentation#
cleopatra.projection
#
Static projected ('globe') map frame for matplotlib axes.
Provides apply_projection_frame -- a single stateless helper that turns
a plain matplotlib.axes.Axes into a static projected map frame: it sets
the projected limits and equal aspect, draws the projection boundary
(the globe's circle, Robinson's rounded rectangle, ...), optionally
clips the existing data layers to that boundary, and draws the
graticule polylines.
The module is pure matplotlib with no PROJ/CRS dependency. It only receives already-computed geometry -- boundary vertices, graticule polylines, and projected limits -- as plain arrays. Whatever produces the projection (reprojecting data and deriving the boundary/graticule) lives upstream; cleopatra only renders the result. This keeps the engine split clean: the upstream owns CRS/PROJ, cleopatra owns matplotlib.
Examples:
Frame a plain axes as an orthographic globe and clip an image to the boundary circle:
>>> import matplotlib
>>> matplotlib.use("Agg")
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> theta = np.linspace(0, 2 * np.pi, 200)
>>> boundary = np.column_stack([np.cos(theta), np.sin(theta)])
>>> fig, ax = plt.subplots()
>>> _ = ax.imshow(np.random.rand(8, 8), extent=(-1, 1, -1, 1))
>>> patch = apply_projection_frame(
... ax, boundary_xy=boundary, xlim=(-1, 1), ylim=(-1, 1)
... )
>>> ax.get_aspect()
1.0
apply_projection_frame(ax, *, boundary_xy, xlim, ylim, graticule_lines=None, clip_artists=True, boundary_kw=None, graticule_kw=None)
#
Turn a plain axes into a static projected ('globe') frame.
Sets equal aspect and the projected x/y limits, draws the projection
boundary as a matplotlib.patches.PathPatch, draws the graticule
polylines, optionally clips the existing data layers to the boundary,
and turns the axis decorations off. The boundary geometry, graticule
polylines, and limits are supplied as plain arrays -- this function
performs no reprojection and has no PROJ/CRS dependency.
This is a one-shot helper: each call appends a fresh boundary patch and a fresh set of graticule lines, so calling it twice on the same axes stacks duplicate artists. Apply it once per axes (create a new axes to re-frame).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Any
|
Matplotlib |
required |
boundary_xy
|
Any
|
|
required |
xlim
|
Sequence[float]
|
Projected-coordinate x-limits |
required |
ylim
|
Sequence[float]
|
Projected-coordinate y-limits |
required |
graticule_lines
|
Sequence[Any] | None
|
Optional list of |
None
|
clip_artists
|
bool
|
If |
True
|
boundary_kw
|
dict[str, Any] | None
|
Style overrides for the boundary patch, merged over
|
None
|
graticule_kw
|
dict[str, Any] | None
|
Style overrides for the graticule lines, merged
over |
None
|
Returns:
| Type | Description |
|---|---|
PathPatch
|
matplotlib.patches.PathPatch: The boundary patch added to the |
PathPatch
|
axes (also used as the clip path). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Examples:
- Frame a plain axes as a globe and read back the result: the
returned patch is registered on the axes and the aspect is equal:
>>> import matplotlib >>> matplotlib.use("Agg") >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from cleopatra.projection import apply_projection_frame >>> theta = np.linspace(0, 2 * np.pi, 200) >>> boundary = np.column_stack([np.cos(theta), np.sin(theta)]) >>> fig, ax = plt.subplots() >>> patch = apply_projection_frame( ... ax, boundary_xy=boundary, xlim=(-1, 1), ylim=(-1, 1) ... ) >>> ax.get_aspect() 1.0 >>> patch in ax.patches True - Clip a data image and draw one graticule line (plain lists are
accepted): the image gains a clip path and one line is added:
>>> import matplotlib >>> matplotlib.use("Agg") >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from cleopatra.projection import apply_projection_frame >>> boundary = [[1, 0], [0, 1], [-1, 0], [0, -1]] >>> meridian = [[0, -1], [0, 1]] >>> fig, ax = plt.subplots() >>> im = ax.imshow(np.zeros((4, 4)), extent=(-1, 1, -1, 1)) >>> patch = apply_projection_frame( ... ax, ... boundary_xy=boundary, ... xlim=(-1, 1), ... ylim=(-1, 1), ... graticule_lines=[meridian], ... ) >>> im.get_clip_path() is not None True >>> len(ax.lines) 1 - Passing a non-Axes object raises
TypeError:
Source code in src/cleopatra/projection.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |