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.
scale_xy([x, y]) Scale element separately across the two axes x and y.
skew([x, y]) Skew the element by x and y degrees
skew_x(x) Skew element along the x-axis by the given angle.
skew_y(y) Skew element along the y-axis by the given angle.
tostr() String representation of the element
copy()[source]

Make a copy 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.

rotate(angle, x=0, y=0)[source]

Rotate element by given angle around given pivot.

Parameters:

angle : float

rotation angle in degrees

x, y : float

pivot coordinates in user coordinate system (defaults to top-left corner of the figure)

scale_xy(x=0, y=None)[source]
Scale element separately across the two axes x and y.
If y is not provided, it is assumed equal to x (according to the W3 specification).
Parameters:

x : float

x-axis scaling factor. To scale down x < 1, scale up x > 1.

y : (optional) float

y-axis scaling factor. To scale down y < 1, scale up y > 1.

skew(x=0, y=0)[source]

Skew the element by x and y degrees Convenience function which calls skew_x and skew_y

Parameters:

x,y : float, float

skew angle in degrees (default 0)

If an x/y angle is given as zero degrees, that transformation is omitted.

skew_x(x)[source]

Skew element along the x-axis by the given angle.

Parameters:

x : float

x-axis skew angle in degrees

skew_y(y)[source]

Skew element along the y-axis by the given angle.

Parameters:

y : float

y-axis skew angle in degrees

tostr()[source]

String representation of the element

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.
scale_xy([x, y]) Scale element separately across the two axes x and y.
skew([x, y]) Skew the element by x and y degrees
skew_x(x) Skew element along the x-axis by the given angle.
skew_y(y) Skew element along the y-axis by the given angle.
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.
scale_xy([x, y]) Scale element separately across the two axes x and y.
skew([x, y]) Skew the element by x and y degrees
skew_x(x) Skew element along the x-axis by the given angle.
skew_y(y) Skew element along the y-axis by the given angle.
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 segments

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.
scale_xy([x, y]) Scale element separately across the two axes x and y.
skew([x, y]) Skew the element by x and y degrees
skew_x(x) Skew element along the x-axis by the given angle.
skew_y(y) Skew element along the y-axis by the given angle.
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.
append(element)[source]

Append new element to the SVG figure

find_id(element_id)[source]

Find elements with the given ID

get_size()[source]

Get figure size

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

save(fname)[source]

Save figure to a file

set_size(size)[source]

Set figure size

to_str()[source]

Returns a string of the SVG figure.

width

Figure width

class svgutils.transform.TextElement(x, y, text, size=8, font='Verdana', weight='normal', letterspacing=0, anchor='start', color='black')[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.
scale_xy([x, y]) Scale element separately across the two axes x and y.
skew([x, y]) Skew the element by x and y degrees
skew_x(x) Skew element along the x-axis by the given angle.
skew_y(y) Skew element along the y-axis by the given angle.
tostr() String representation of the element
svgutils.transform.from_mpl(fig, savefig_kw)[source]

Create a SVG figure from a matplotlib figure.

Parameters:

fig : matplotlib.Figure instance

savefig_kw : dict

keyword arguments to be passed to matplotlib’s savefig

Returns:

SVGFigure

newly created SVGFigure initialised with the string content.

Examples

If you want to overlay the figure on another SVG, you may want to pass the transparent option:

>>> from svgutils import transform
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> line, = plt.plot([1,2])
>>> svgfig = transform.from_mpl(fig, 
...              savefig_kw=dict(transparent=True))
>>> svgfig.getroot()
<svgutils.transform.GroupElement object at ...>
svgutils.transform.fromfile(fname)[source]

Open SVG figure from file.

Parameters:

fname : str

name of the SVG file

Returns:

SVGFigure

newly created SVGFigure initialised with the file content

svgutils.transform.fromstring(text)[source]

Create a SVG figure from a string.

Parameters:

text : str

string representing the SVG content. Must be valid SVG.

Returns:

SVGFigure

newly created SVGFigure initialised with the string content.