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
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)

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.
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 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.
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)[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.

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.