save_svg
save_svg(path, shapes, [width=800], [height=600])
Input: path, shapes, width, height · Output: no output
What it is
path is a string destination; shapes is a List of shape records (a single record on its own is also accepted). Writes a standalone SVG document with an XML declaration, the SVG namespace, and both width/height and a matching viewBox — a viewBox alone renders at the container's size rather than the file's own. The canvas is not auto-fitted to the shapes: a text run's rendered width depends on a font this writer does not load, so an automatic canvas would be right for the geometry and silently clip the labels. Pass width=/height= for a tight canvas.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
save_svg(path, shapes, [width=800], [height=600]) | path stringpath is a string destination.shapes Listshapes is a List of shape records (a single record on its own is also accepted).width optional (default 800)Writes a standalone SVG document with an XML declaration, the SVG namespace, and both width/height and a matching viewBox — a viewBox alone renders at the container's size rather than the file's own.height optional (default 600)Writes a standalone SVG document with an XML declaration, the SVG namespace, and both width/height and a matching viewBox — a viewBox alone renders at the container's size rather than the file's own. | no output | — |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
import svg
shapes = [
svg.rect(10, 10, 100, 50, fill = "#e74c3c"),
svg.circle(200, 60, 40, fill = "#3498db", stroke = "#000000", stroke_width = 2),
svg.line(0, 0, 300, 120, stroke = "#333333", stroke_width = 1.5),
svg.path("M 10 100 L 60 40 L 110 100 Z", stroke = "#2ecc71", stroke_width = 2),
svg.text(20, 150, "R&D output", font_size = 16, fill = "#111111")]
save_svg("drawing.svg", shapes, width = 320, height = 180)
doc = load_svg("drawing.svg")
print(doc.width) # 320
print(len(doc.shapes)) # 5
print(doc.shapes[1].kind) # circle
print(doc.shapes[1].r) # 40
320 5 circle 40
from images
How it works
save_svg(path, shapes, [width=], [height=]) / load_svg(path) -- VECTOR I/O, the counterpart to the raster pair above and named to match it. Note this is NOT savefig("f.svg"), which renders the current FIGURE through plotting::render_svg and is unaffected by any of this; see svg_io's module doc for the distinction.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.