2.1. transform
– basic SVG transformations¶
This module implements low-level API allowing to open and manipulate SVG files. An example use is described in the Creating plublication-quality figures tutorial.
-
class
svgutils.transform.
FigureElement
(xml_element, defs=None)[source]¶ Base class representing single figure element
Methods
copy
()Make a copy of the element find_id
(element_id)Find element by its id. moveto
(x, y[, scale])Move and scale element. rotate
(angle[, x, y])Rotate element by given angle around given pivot. tostr
()String representation of the element -
find_id
(element_id)[source]¶ Find element by its id.
Parameters: element_id : str
ID of the element to find
Returns: FigureElement
one of the children element with the given ID.
-
moveto
(x, y, scale=1)[source]¶ Move and scale element.
Parameters: x, y : float
displacement in x and y coordinates in user units (‘px’).
scale : float
scaling factor. To scale down scale < 1, scale up scale > 1. For no scaling scale = 1.
-
-
class
svgutils.transform.
GroupElement
(element_list, attrib=None)[source]¶ Group element.
Container for other elements. Corresponds to SVG
<g>
tag.Methods
copy
()Make a copy of the element find_id
(element_id)Find element by its id. moveto
(x, y[, scale])Move and scale element. rotate
(angle[, x, y])Rotate element by given angle around given pivot. tostr
()String representation of the element
-
class
svgutils.transform.
ImageElement
(stream, width, height, format='png')[source]¶ Inline image element.
Correspoonds to SVG
<image>
tag. Image data encoded as base64 string.Methods
copy
()Make a copy of the element find_id
(element_id)Find element by its id. moveto
(x, y[, scale])Move and scale element. rotate
(angle[, x, y])Rotate element by given angle around given pivot. tostr
()String representation of the element
-
class
svgutils.transform.
LineElement
(points, width=1, color='black')[source]¶ Line element.
Corresponds to SVG
<path>
tag. It handles only piecewise straight segmentsMethods
copy
()Make a copy of the element find_id
(element_id)Find element by its id. moveto
(x, y[, scale])Move and scale element. rotate
(angle[, x, y])Rotate element by given angle around given pivot. tostr
()String representation of the element
-
class
svgutils.transform.
SVGFigure
(width=None, height=None)[source]¶ SVG Figure.
It setups standalone SVG tree. It corresponds to SVG
<svg>
tag.Attributes
height
Figure height width
Figure width Methods
append
(element)Append new element to the SVG figure find_id
(element_id)Find elements with the given ID get_size
()Get figure size getroot
()Return the root element of the figure. save
(fname)Save figure to a file set_size
(size)Set figure size to_str
()Returns a string of the SVG figure. -
getroot
()[source]¶ Return the root element of the figure.
The root element is a group of elements after stripping the toplevel
<svg>
tag.Returns: GroupElement
All elements of the figure without the
<svg>
tag.
-
height
¶ Figure height
-
width
¶ Figure width
-
-
class
svgutils.transform.
TextElement
(x, y, text, size=8, font='Verdana', weight='normal', letterspacing=0)[source]¶ Text element.
Corresponds to SVG
<text>
tag.Methods
copy
()Make a copy of the element find_id
(element_id)Find element by its id. moveto
(x, y[, scale])Move and scale element. rotate
(angle[, x, y])Rotate element by given angle around given pivot. tostr
()String representation of the element
-
svgutils.transform.
from_mpl
(fig)[source]¶ Create a SVG figure from a
matplotlib
figure.Parameters: fig : matplotlib.Figure instance
Returns: SVGFigure
newly created
SVGFigure
initialised with the string content.