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.
Class Documentation#
cleopatra.polygon_glyph.PolygonGlyph
#
Bases: Glyph
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.
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
|
Pre-existing axes to draw on. Default is None. |
None
|
fig
|
Figure
|
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.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.glyph.Glyph._prepare_scalar_mapping: Shared norm/colorbar/ticks pipeline used for the filled path.
Source code in src/cleopatra/polygon_glyph.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 | |
plot(outline_only=False, ax=None, title=None, add_colorbar=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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outline_only
|
bool
|
Draw unfilled outlines even when |
False
|
ax
|
Axes
|
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
|
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:
>>> import numpy as np >>> from cleopatra.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
Source code in src/cleopatra/polygon_glyph.py
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 | |
Examples#
Value-filled polygons#
import numpy as np
from cleopatra.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")