How to use the svgutils.transform.SVGFigure function in svgutils

To help you get started, we’ve selected a few svgutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github poldracklab / niworkflows / niworkflows / viz / utils.py View on Github external
viewbox = [float(v) for v in f.root.get("viewBox").split(" ")]
        width = int(viewbox[2])
        height = int(viewbox[3])
        sizes.append((width, height))
    nsvgs = len(bg_svgs)

    sizes = np.array(sizes)

    # Calculate the scale to fit all widths
    width = sizes[ref, 0]
    scales = width / sizes[:, 0]
    heights = sizes[:, 1] * scales

    # Compose the views panel: total size is the width of
    # any element (used the first here) and the sum of heights
    fig = SVGFigure(width, heights[:nsvgs].sum())

    yoffset = 0
    for i, r in enumerate(roots):
        r.moveto(0, yoffset, scale=scales[i])
        if i == (nsvgs - 1):
            yoffset = 0
        else:
            yoffset += heights[i]

    # Group background and foreground panels in two groups
    if fg_svgs:
        newroots = [
            GroupElement(roots[:nsvgs], {"class": "background-svg"}),
            GroupElement(roots[nsvgs:], {"class": "foreground-svg"}),
        ]
    else:
github poldracklab / mriqc / mriqc / viz / svg.py View on Github external
elif axis == "horizontal":
        # Calculate the scale to fit all heights
        scales = [1.0] * len(svgs)
        if not all([height[0] == sizes[0][1] for height in sizes[1:]]):
            ref_size = sizes[0]
            for i, els in enumerate(sizes):
                scales[i] = ref_size[1] / els[1]

        newsizes = [
            tuple(size) for size in np.array(sizes) * np.array(scales)[..., np.newaxis]
        ]
        totalsize = [np.sum(newsizes, axis=0)[0], newsizes[0][1]]

    # Compose the views panel: total size is the width of
    # any element (used the first here) and the sum of heights
    fig = svgt.SVGFigure(totalsize[0], totalsize[1])

    if axis == "vertical":
        yoffset = 0
        for i, r in enumerate(roots):
            size = newsizes[i]
            r.moveto(0, yoffset, scale=scales[i])
            yoffset += size[1]
            fig.append(r)
    elif axis == "horizontal":
        xoffset = 0
        for i, r in enumerate(roots):
            size = newsizes[i]
            r.moveto(xoffset, 0, scale=scales[i])
            xoffset += size[0]
            fig.append(r)
github btel / svg_utils / docs / source / tutorials / fig_final.py View on Github external
import svgutils.transform as sg
import sys 

#create new SVG figure
fig = sg.SVGFigure("16cm", "6.5cm")

# load matpotlib-generated figures
fig1 = sg.fromfile('sigmoid_fit.svg')
fig2 = sg.fromfile('anscombe.svg')

# get the plot objects
plot1 = fig1.getroot()
plot2 = fig2.getroot()
plot2.moveto(280, 0, scale=0.5)

# add text labels
txt1 = sg.TextElement(25,20, "A", size=12, weight="bold")
txt2 = sg.TextElement(305,20, "B", size=12, weight="bold")

# append plots and labels to figure
fig.append([plot1, plot2])
github btel / svg_utils / src / svgutils / compose.py View on Github external
def tostr(self):
        """Export SVG as a string"""
        element = _transform.SVGFigure(self.width, self.height)
        element.append(self)
        svgstr = element.to_str()
        return svgstr
github btel / svg_utils / src / svgutils / compose.py View on Github external
def save(self, fname):
        """Save figure to SVG file.

        Parameters
        ----------
        fname : str
            Full path to file.
        """
        element = _transform.SVGFigure(self.width, self.height)
        element.append(self)
        element.save(os.path.join(CONFIG['figure.save_path'], fname))
github btel / svg_utils / src / svgutils / templates.py View on Github external
#!/usr/bin/env python
#coding=utf-8

from svgutils.transform import SVGFigure, GroupElement

class BaseTemplate(SVGFigure):

    def __init__(self):
        SVGFigure.__init__(self)
        self.figures = []

    def add_figure(self, fig):
        w, h =  fig.get_size()
        root = fig.getroot()
        self.figures.append({'root': root,
                             'width': w,
                             'height' : h})

    def _transform(self):
        pass

    def save(self, fname):
github gkichaev / PAINTOR_V3.0 / CANVIS / CANVIS.py View on Github external
size_annotation_plot = 30
    size_heatmap = 200

    if heatmaps == None:
        horizontal = 'n'
    elif len(heatmaps)>1:
        horizontal='y'

    if horizontal == 'y':
        size_width = "9in"
        size_height = '9in'
    else:
        size_width = "5in"
        size_height = '11in'

    fig = sg.SVGFigure(size_width, size_height)
    value_plots.savefig('value_plots.svg', format='svg', dpi=DPI, transparent=True)
    value_plots = sg.fromfile('value_plots.svg')
    plot1 = value_plots.getroot()
    if annotation_plot is not None:
        len_ann_plot = (len(annotation_plot))
    else:
        len_ann_plot = 0
    plot1.moveto(0, 0)
    fig.append(plot1)
    # plot heatmap(s)
    len_annotation_plot = size_annotation_plot * (len_ann_plot + 1)
    if heatmaps is not None:
        heatmap_count = 0
        for heatmap in heatmaps:
            plot4 = heatmap[0]
            try:
github chrisjbillington / git_nautilus_icons / icons / generate_icons.py View on Github external
br_file = 'sub_icons/{}-r.svg'.format(br)
    br_image = sg.fromfile(br_file).getroot()
    br_image.moveto(-16, -16, scale=1.5)
    background_image.append(br_image)

    filename = '-'.join([name for name in (tl, tr, bl, br) if name is not None])
    filename = 'hicolor/16x16/emblems/git-{}.svg'.format(filename)
    background_image.save(filename)


# Simplified icons for the 8x8@2 size. Only shows a single icon for the worktree part of
# the status.
os.system('mkdir -p hicolor/8x8@2/emblems/')
for tl, tr, bl, br in all_icons:
    # create new SVG figure
    background_image = sg.SVGFigure(32, 32)
    if 'unmerged' in br:
        br_file = 'sub_icons/unmerged-simple.svg'
    else:
        br_file = 'sub_icons/{}-r.svg'.format(br)
    br_image = sg.fromfile(br_file).getroot()
    br_image.moveto(-32, -32, scale=2)
    background_image.append(br_image)

    filename = '-'.join([name for name in (tl, tr, bl, br) if name is not None])
    filename = 'hicolor/8x8@2/emblems/git-{}.svg'.format(filename)
    background_image.save(filename)


# Simplified, hand-drawn icons for the 8x8 size. Only shows a single icon for the
# worktree part of the status.
os.system('mkdir -p hicolor/8x8/emblems/')