VectorGlyph Class#
The VectorGlyph class renders a 2-D (u, v) vector field over (x, y)
positions as arrows (quiver), wind barbs (barbs), or streamlines
(streamplot). The artist is coloured by the per-vector magnitude
hypot(u, v) through the shared scalar-mapping pipeline, with a matching
colorbar.
VectorGlyph(..., thin=n) draws every nth grid point for quiver / barbs, and
plot(compose=True) lays the arrows over an existing layer instead of replacing it — see
Render options.
Class Documentation#
cleopatra.glyphs.gridded.vector_glyph.VectorGlyph
#
Visualization class for 2D vector fields.
Renders a (u, v) vector field over (x, y) positions as arrows,
wind barbs, or streamlines, with the artist coloured by the vector
magnitude hypot(u, v) through the shared scalar-mapping pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x-coordinates of the vector positions. |
required |
y
|
ndarray
|
y-coordinates of the vector positions. |
required |
u
|
ndarray
|
x-components of the vectors. Must broadcast against |
required |
v
|
ndarray
|
y-components of the vectors. Must broadcast against |
required |
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 |
{}
|
Examples:
- Build a field and inspect the stored magnitude:
See Also
cleopatra.glyphs.base.glyph.Glyph._prepare_scalar_mapping: Shared norm/colorbar/ticks pipeline used to colour by magnitude.
Source code in src/cleopatra/glyphs/gridded/vector_glyph.py
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | |
magnitude
property
#
Per-vector magnitude hypot(u, v) used for colour mapping.
add_key(im, x=0.9, y=1.02, value=10.0, label=None, labelpos='E', **kwargs)
#
Add a reference-arrow key to a quiver plot.
Wraps Axes.quiverkey to draw a sample arrow of known length
with a text label, the standard legend for a quiver field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
im
|
The |
required | |
x
|
float
|
Key x-position in axes fraction coordinates. Default is 0.9. |
0.9
|
y
|
float
|
Key y-position in axes fraction coordinates. Default is 1.02. |
1.02
|
value
|
float
|
The reference vector length the key represents. Default is 10.0. |
10.0
|
label
|
str | None
|
Text drawn beside the key. Default is |
None
|
labelpos
|
str
|
Side of the arrow for the label ( |
'E'
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
QuiverKey |
QuiverKey
|
The created key artist. |
Examples:
- Add a 5 m/s reference key to a quiver:
>>> import numpy as np >>> from cleopatra.glyphs.gridded.vector_glyph import VectorGlyph >>> x, y = np.meshgrid(np.arange(3), np.arange(3)) >>> u = np.ones_like(x, dtype=float) >>> v = np.ones_like(y, dtype=float) >>> glyph = VectorGlyph(x, y, u, v) >>> fig, ax, im = glyph.plot(kind="quiver") >>> key = glyph.add_key(im, value=5.0, label="5 m/s") >>> key.text.get_text() '5 m/s'
Source code in src/cleopatra/glyphs/gridded/vector_glyph.py
plot(kind='quiver', ax=None, title=None, add_colorbar=None, colorbar=None, color=None, contour=None, classify=None, compose=False)
#
Render the vector field, coloured by magnitude.
Dispatches to Axes.quiver, Axes.barbs, or Axes.streamplot
based on kind. The colour scale, norm, ticks, and colorbar are
resolved from the magnitude via _prepare_scalar_mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
One of |
'quiver'
|
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
|
compose
|
bool
|
Draw over whatever is already on |
False
|
Returns:
| Type | Description |
|---|---|
|
tuple[Figure, Axes, Any]: The figure, the axes, and the
mappable artist (the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
- A barbs plot returns the Barbs mappable with the magnitude
array:
>>> import numpy as np >>> from cleopatra.glyphs.gridded.vector_glyph import VectorGlyph >>> x, y = np.meshgrid(np.arange(3), np.arange(3)) >>> u = np.full_like(x, 2.0, dtype=float) >>> v = np.zeros_like(y, dtype=float) >>> glyph = VectorGlyph(x, y, u, v) >>> fig, ax, im = glyph.plot(kind="barbs") >>> float(im.get_array().max()) 2.0 - Arrows composed over a host raster draw on the host's own axes and
add no colorbar, so the figure keeps the one axes it had:
>>> import matplotlib >>> matplotlib.use("Agg") >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from cleopatra.glyphs.gridded.vector_glyph import VectorGlyph >>> host_fig, host_ax = plt.subplots() >>> _ = host_ax.imshow(np.arange(9.0).reshape(3, 3), extent=[0, 2, 0, 2]) >>> x, y = np.meshgrid(np.arange(3), np.arange(3)) >>> u = np.full_like(x, 1.0, dtype=float) >>> v = np.full_like(y, 1.0, dtype=float) >>> fig, ax, im = VectorGlyph(x, y, u, v).plot( ... kind="quiver", ax=host_ax, compose=True ... ) >>> len(fig.axes) 1 >>> ax is host_ax True >>> plt.close(host_fig) - Asking for the overlay's colorbar brings it back:
>>> import matplotlib >>> matplotlib.use("Agg") >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from cleopatra.glyphs.gridded.vector_glyph import VectorGlyph >>> host_fig, host_ax = plt.subplots() >>> _ = host_ax.imshow(np.arange(9.0).reshape(3, 3), extent=[0, 2, 0, 2]) >>> x, y = np.meshgrid(np.arange(3), np.arange(3)) >>> u = np.full_like(x, 1.0, dtype=float) >>> v = np.full_like(y, 1.0, dtype=float) >>> fig, ax, im = VectorGlyph(x, y, u, v).plot( ... kind="quiver", ax=host_ax, compose=True, add_colorbar=True ... ) >>> len(fig.axes) 2 >>> plt.close(host_fig) - An unknown kind raises ValueError:
>>> import numpy as np >>> from cleopatra.glyphs.gridded.vector_glyph import VectorGlyph >>> x, y = np.meshgrid(np.arange(2), np.arange(2)) >>> glyph = VectorGlyph(x, y, np.ones_like(x), np.ones_like(y)) >>> glyph.plot(kind="spirograph") Traceback (most recent call last): ... ValueError: unknown vector kind 'spirograph'; expected one of ...
Source code in src/cleopatra/glyphs/gridded/vector_glyph.py
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
Examples#
Arrows (quiver)#
import numpy as np
from cleopatra.glyphs.gridded.vector_glyph import VectorGlyph
x, y = np.meshgrid(np.linspace(0, 1, 8), np.linspace(0, 1, 8))
u, v = np.cos(x * np.pi), np.sin(y * np.pi)
vg = VectorGlyph(x, y, u, v)
fig, ax, artist = vg.plot(kind="quiver", title="Vector field")