API reference¶
Everything below is importable directly from the top-level svg2mpl package.
Rendering¶
add_svg ¶
add_svg(
source: str | Path,
xy: tuple[float, float] = (0, 0),
width: float | None = None,
height: float | None = None,
scale: tuple[float, float] | float = (1, 1),
origin: tuple[float, float] = (0.5, 0.5),
rotation: float = 0,
transform=None,
flip_y: bool = True,
place: bool = True,
exclude: str | Iterable[str] = (),
bbox_exclude: str | Iterable[str] = (),
grayscale: bool = False,
force_patches: bool = False,
ax=None,
per_path_kwargs: Mapping[str, Mapping[str, Any]]
| None = None,
**global_kwargs: Any,
)
Render an SVG into a matplotlib axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str or Path
|
Path to an SVG file, or a string of SVG markup (must start with |
required |
xy
|
tuple
|
Where to place the drawing's |
(0, 0)
|
width
|
float or None
|
Final size of the drawing's bounding box. If only one is given the other
is derived to preserve the aspect ratio. If both are |
None
|
height
|
float or None
|
Final size of the drawing's bounding box. If only one is given the other
is derived to preserve the aspect ratio. If both are |
None
|
scale
|
float or tuple
|
An extra scaling factor applied on top of |
(1, 1)
|
origin
|
tuple
|
Anchor point inside the bounding box, normalized (default center). |
(0.5, 0.5)
|
rotation
|
float
|
Rotation in degrees. |
0
|
transform
|
Transform
|
A matplotlib transform to compose with (default |
None
|
flip_y
|
bool
|
Flip SVG's y-down axis to matplotlib's y-up so the drawing appears as in
a browser. Set |
True
|
place
|
bool
|
When |
True
|
exclude
|
str or Iterable[str]
|
Regular expressions; paths whose id matches are not drawn. |
()
|
bbox_exclude
|
str or Iterable[str]
|
Regular expressions for paths to ignore when computing the placement bounding box. |
()
|
grayscale
|
bool
|
Convert fill colors to grayscale. |
False
|
force_patches
|
bool
|
Return individual :class: |
False
|
ax
|
Axes
|
Axes to draw on (default current axes). |
None
|
per_path_kwargs
|
Mapping of Mapping
|
Per-path overrides: |
None
|
**global_kwargs
|
Any
|
Keyword arguments applied to every path. |
{}
|
Returns:
| Type | Description |
|---|---|
PathCollection or dict
|
A |
Source code in src/svg2mpl/render.py
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 | |
Loading¶
load ¶
Parse an SVG file into a (cached) :class:~svg2mpl.scene.Scene.
Paths in the returned scene are in SVG user space (y points down); the
optional y-flip is applied by :func:add_svg. The result is shared and
cached -- callers must not mutate it.
Source code in src/svg2mpl/render.py
load_string ¶
Scene model¶
Scene
dataclass
¶
Scene(
shapes: list[Shape] = list(),
texts: list[Text] = list(),
width: float = 0.0,
height: float = 0.0,
gradients: dict[str, Any] = dict(),
)
A parsed SVG: its drawables, size, and gradient definitions.
Shape
dataclass
¶
A filled/stroked path with its resolved matplotlib style.
Text
dataclass
¶
A piece of text with its anchor point and resolved style.
Geometry helpers¶
These lower-level helpers back add_svg and are exposed for advanced use.
get_bbox ¶
Return the bounding box enclosing a collection of paths.
Source code in src/svg2mpl/render.py
get_affine_matrix ¶
get_affine_matrix(
bbox: Bbox,
source_xy: tuple[float, float],
target_xy: tuple[float, float],
rotation: float,
width: float | None,
height: float | None,
scale: tuple[float, float] | float = (1.0, 1.0),
) -> Affine2D
Return an affine that places bbox at target_xy in data coords.
source_xy is the anchor inside the bounding box in normalized coordinates
((0, 0) lower-left, (1, 1) upper-right). width/height set the
final size (None preserves aspect ratio); scale is an extra factor;
rotation is in degrees.
Source code in src/svg2mpl/render.py
get_is_included ¶
Return whether id_ survives the exclude regular expressions.
vectorize_dicts ¶
Transpose a mapping of dicts into a dict of lists for PathCollection.
Returns None if the input is empty or the property dicts do not share the
same keys (so a single PathCollection cannot represent them).