PolygonGlyph Class#
The PolygonGlyph class wraps matplotlib.collections.PolyCollection. With a
per-polygon values array the polygons are filled and colour-mapped through the
shared scalar-mapping pipeline and a colorbar is attached. With no values (or
outline_only=True) only the polygon outlines are drawn.
The edgecolor option defaults to "none" so a value-filled choropleth renders
borderless. Outline mode substitutes cleopatra.glyphs.primitives.polygon_glyph.OUTLINE_EDGECOLOR
(black) for that default, since an unfilled polygon with a transparent edge would
be invisible; pass an explicit edgecolor to override it.
Class Documentation#
cleopatra.glyphs.primitives.polygon_glyph.PolygonGlyph
#
Visualization class for collections of polygons.
Wraps matplotlib.collections.PolyCollection. With a per-polygon
values array, polygons are filled and colour-mapped through the
shared scalar-mapping pipeline and a colorbar is attached. With no
values (or outline_only=True), only the polygon outlines are
drawn, in OUTLINE_EDGECOLOR unless edgecolor is given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygons
|
Sequence[ndarray]
|
Sequence of polygons, each an |
required |
values
|
ndarray | None
|
Optional 1D array of per-polygon scalar values for colour mapping. Must match the number of polygons when given. Default is None (outline-only). |
None
|
ax
|
Axes | None
|
Pre-existing axes to draw on. Default is None. |
None
|
fig
|
Figure | None
|
Pre-existing figure. Default is None. |
None
|
**kwargs
|
Override any key in |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
- Inspect the value array carried by the collection:
>>> import numpy as np >>> from cleopatra.glyphs.primitives.polygon_glyph import PolygonGlyph >>> polys = [ ... np.array([[0.0, 0.0], [1.0, 0.0], [0.5, 1.0]]), ... np.array([[1.0, 0.0], [2.0, 0.0], [1.5, 1.0]]), ... ] >>> glyph = PolygonGlyph(polys, values=np.array([3.0, 7.0])) >>> fig, ax, pc = glyph.plot() >>> [float(v) for v in pc.get_array()] [3.0, 7.0]
See Also
cleopatra.glyphs.base.glyph.Glyph._prepare_scalar_mapping: Shared norm/colorbar/ticks pipeline used for the filled path.
Source code in src/cleopatra/glyphs/primitives/polygon_glyph.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
plot(outline_only=False, ax=None, title=None, add_colorbar=None, colorbar=None, color=None, contour=None, classify=None)
#
Draw the polygons, filling by value when present.
When values was supplied and outline_only is False, the
polygons are filled and colour-mapped through
_prepare_scalar_mapping (so vmin / vmax / levels /
color_scale apply) with a matching colorbar. Otherwise only
the outlines are drawn and no colorbar is added; the outlines
use OUTLINE_EDGECOLOR when edgecolor is left at its
borderless-fill default of "none".
The one exception is scheme="categorical": vmin / vmax /
levels / color_scale are ignored (with a warning if set), and
instead of a colorbar a disjoint_legend is drawn and stored on
self.category_legend (self.cbar stays None). See
Glyph._prepare_categorical_mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outline_only
|
bool
|
Draw unfilled outlines even when |
False
|
ax
|
Axes | None
|
Axes to draw on. Falls back to the axes supplied at construction, otherwise a new figure/axes is created. |
None
|
title
|
str | None
|
Plot title. Overrides |
None
|
add_colorbar
|
bool | None
|
Override the |
None
|
colorbar
|
bool | ColorBar | None
|
Typed |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Figure, Axes, PolyCollection]
|
tuple[Figure, Axes, PolyCollection]: The figure, the axes,
and the |
Examples:
- Outline-only mode carries no colour array and no colorbar,
and its edges are opaque so the outlines are visible:
>>> import numpy as np >>> from cleopatra.glyphs.primitives.polygon_glyph import PolygonGlyph >>> polys = [np.array([[0.0, 0.0], [1.0, 0.0], [0.5, 1.0]])] >>> glyph = PolygonGlyph(polys, values=np.array([5.0])) >>> fig, ax, pc = glyph.plot(outline_only=True) >>> pc.get_array() is None True >>> glyph.cbar is None True >>> float(pc.get_edgecolor()[0][3]) # alpha 1.0 - An explicit
edgecoloris honoured as given:
Source code in src/cleopatra/glyphs/primitives/polygon_glyph.py
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
Examples#
Value-filled polygons#
import numpy as np
from cleopatra.glyphs.primitives.polygon_glyph import PolygonGlyph
polygons = [
np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]),
np.array([[1.0, 0.0], [2.0, 0.0], [2.0, 1.0], [1.0, 1.0]]),
np.array([[0.0, 1.0], [1.0, 1.0], [0.5, 2.0]]),
]
values = np.array([10.0, 20.0, 30.0])
pg = PolygonGlyph(polygons, values=values)
fig, ax, pc = pg.plot(title="Polygons by value")

Outlines only#
pg = PolygonGlyph(polygons)
fig, ax, pc = pg.plot(outline_only=True)
# ... or pick the outline colour and width
pg = PolygonGlyph(polygons, edgecolor="navy", linewidth=1.5)
fig, ax, pc = pg.plot(outline_only=True)
