Ejemplo n.º 1
0
    def join(self, verbose=True):
        """
        join :class:`matplotlib.figure.Figure` objects contained in
        the object.  Currently supports only joining into a single
        horizontal row.

        ARGS:
            verbose (boolean): if True, print a message to stdout as
            each figure is added to the SVG file.

        .. warning::
            for now assumes that all figures are the same size.
        """

        # TODO ([email protected]): sniff out the figure sizes and
        # size the SVG figure to accommodate all the figures.

        # TODO ([email protected]): allow user to specify rows,
        # columns of combined SVG figures.

        layout = VerticalLayout()
        sz = map(int, sg.from_mpl(self.figs[0]).get_size())
        sz[1] *= len(self.figs)
        sz = map(str, sz)
        layout.set_size(sz)
        for f in self.figs:
            layout.add_figure(sg.from_mpl(f))

        if verbose:
            print('saving {}'.format(self.fname_svg))
        layout.save(self.fname_svg)
Ejemplo n.º 2
0
matplotlib.use('AGG')

import svgutils.transform as sg
from svgutils.templates import VerticalLayout
import matplotlib.pyplot as plt
import numpy as np
import os
import os.path

try:
    figs = []
    for i in range(2):
        figs.append(plt.figure())
        plt.plot(np.random.random(100))

    layout = VerticalLayout()
    sz = map(int, sg.from_mpl(figs[0]).get_size())
    sz[1] *= 3
    sz = map(str, sz)
    layout.set_size(sz)
    layout.add_figure(sg.from_mpl(figs[0]))
    layout.add_figure(sg.from_mpl(figs[1]))

    txt1 = sg.TextElement(50, 50, "HELLO", size=12)
    layout.append([txt1])
    layout.save(os.path.join('/', 'tmp', 'stack_plots.svg'))
    try:
        print('converting to pdf')
        subprocess.call(
            '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --export-pdf=/tmp/stack_plots.pdf /tmp/stack_plots.svg',
            shell=True)
Ejemplo n.º 3
0
#!/usr/bin/env python
# coding=utf-8

import numpy as np
import matplotlib.pyplot as plt

from svgutils.transform import from_mpl
from svgutils.templates import VerticalLayout

layout = VerticalLayout()

fig1 = plt.figure()
plt.plot([1, 2])
fig2 = plt.figure()
plt.plot([2, 1])

layout.add_figure(from_mpl(fig1))
layout.add_figure(from_mpl(fig2))

layout.save("stack_plots.svg")
Ejemplo n.º 4
0
#!/usr/bin/env python
#coding=utf-8

import numpy as np
import matplotlib.pyplot as plt

from svgutils.transform import from_mpl
from svgutils.templates import VerticalLayout

layout = VerticalLayout()

fig1 = plt.figure()
plt.plot([1,2])
fig2 = plt.figure()
plt.plot([2,1])

layout.add_figure(from_mpl(fig1))
layout.add_figure(from_mpl(fig2))

print from_mpl(fig1).get_size()
layout.save('stack_plots.svg')

Ejemplo n.º 5
0
def chicpeaDownload(request, url):
    queryDict = request.POST
    output_format = queryDict.get("output_format")
    CSS = queryDict.get("css-styles")
    WIDTH = int(queryDict.get("svg-width")) + 40 + 50
    HEIGHT = int(queryDict.get("svg-height")) + 100
    tissue = queryDict.get("tissue").replace(' ', '_')
    returnFileName = 'CHiCP-' + queryDict.get("searchTerm") + '-' + tissue + '.' + output_format

    fig1 = fromstring(queryDict.get("data-main"))
    layout = ColumnLayout(1)
    layout.add_figure(fig1)

    if queryDict.get("data-bait") and queryDict.get("data-target"):
        s1 = queryDict.get("data-bait")
        s2 = queryDict.get("data-target")
        layoutPanels = VerticalLayout()
        layoutPanels.add_figure(fromstring(s1))
        layoutPanels.add_figure(fromstring(s2))
        layoutPanels._generate_layout()
        svgPanels = layoutPanels.to_str()
        fig2 = fromstring(svgPanels)
    else:
        fig2 = fromstring("<svg></svg>")

    layout.add_figure(fig2)
    layout._generate_layout()
    SVG = layout.to_str().decode()
    p = re.compile(r'translate\((\d+), 0\)')
    m = p.search(SVG)
    SVG = re.sub(r'translate\(\d+, 0\)', r'translate('+str(int(m.group(1)) + 60)+', 50)', SVG)
    SVG = SVG.replace('translate(0, 270)', 'translate(0, 390)')

    SVG = SVG.replace('<g>', '<g transform="translate(20,50) scale(1)">', 1)
    SVG = SVG.replace('<svg ', '<svg style="width:'+str(WIDTH)+'px;height:'+str(HEIGHT)+'px;" ')
    SVG = SVG.replace("</svg>", '<defs><style type="text/css">'+CSS+'</style></defs></svg>')

    if output_format == "svg":
        response = HttpResponse(content_type='image/svg+xml')
        response['Content-Disposition'] = 'attachment; filename="' + returnFileName + '"'
        response.write(SVG)
    elif output_format == "pdf" or output_format == "png":
        mime_type = "application/x-pdf" if output_format == "pdf" else "image/png"

        response = HttpResponse(content_type=mime_type)
        response['Content-Disposition'] = 'attachment; filename="' + returnFileName + '"'
        iFile = NamedTemporaryFile(delete=False)
        oFile = NamedTemporaryFile(delete=False)
        iFile.write(SVG.encode())
        iFile.close()

        if output_format == "pdf":
            svg2pdf(SVG.encode('utf-8'), write_to=str(oFile.name))
        else:
            svg2png(SVG.encode('utf-8'), write_to=str(oFile.name))

        fileData = oFile.read()
        response.write(fileData)
    else:
        retJSON = {"error": "output format was not recognised"}
        response = JsonResponse(retJSON)
    return response

import svgutils.transform as sg
from svgutils.templates import VerticalLayout
import matplotlib.pyplot as plt
import numpy as np
import os
import os.path

try:
    figs = []
    for i in range(2):
        figs.append(plt.figure())
        plt.plot(np.random.random(100))

    layout = VerticalLayout()
    sz = map(int, sg.from_mpl(figs[0]).get_size())
    sz[1] *= 3
    sz = map(str, sz)
    layout.set_size(sz)
    layout.add_figure(sg.from_mpl(figs[0]))
    layout.add_figure(sg.from_mpl(figs[1]))

    txt1 = sg.TextElement(50, 50, "HELLO", size=12)
    layout.append([txt1])
    layout.save(os.path.join('/', 'tmp', 'stack_plots.svg'))
    try:
        print('converting to pdf')
        subprocess.call('/Applications/Inkscape.app/Contents/Resources/bin/inkscape --export-pdf=/tmp/stack_plots.pdf /tmp/stack_plots.svg', shell=True)
    except:
        print('unable to run inkscape')
Ejemplo n.º 7
0
def chicpeaDownload(request, url):
    queryDict = request.POST
    output_format = queryDict.get("output_format")
    CSS = queryDict.get("css-styles")
    WIDTH = int(queryDict.get("svg-width")) + 40 + 50
    HEIGHT = int(queryDict.get("svg-height")) + 60
    tissue = queryDict.get("tissue").replace(' ', '_')
    returnFileName = 'CHiCP-' + queryDict.get("searchTerm") + '-' + tissue + '.' + output_format

    fig1 = fromstring(queryDict.get("data-main"))
    layout = ColumnLayout(1)
    layout.add_figure(fig1)

    if queryDict.get("data-bait") and queryDict.get("data-target"):
        s1 = queryDict.get("data-bait")
        s2 = queryDict.get("data-target")
        layoutPanels = VerticalLayout()
        layoutPanels.add_figure(fromstring(s1))
        layoutPanels.add_figure(fromstring(s2))
        layoutPanels._generate_layout()
        svgPanels = layoutPanels.to_str()
        fig2 = fromstring(svgPanels)
    else:
        fig2 = fromstring("<svg></svg>")

    layout.add_figure(fig2)
    layout._generate_layout()
    SVG = layout.to_str().decode()
    p = re.compile(r'translate\((\d+), 0\)')
    m = p.search(SVG)
    SVG = re.sub(r'translate\(\d+, 0\)', r'translate('+str(int(m.group(1)) + 60)+', 50)', SVG)
    SVG = SVG.replace('translate(0, 270)', 'translate(0, 390)')

    SVG = SVG.replace('<g>', '<g transform="translate(20,30) scale(1)">', 1)
    SVG = SVG.replace('<svg ', '<svg style="width:'+str(WIDTH)+'px;height:'+str(HEIGHT)+'px;" ')
    SVG = SVG.replace("</svg>", '<defs><style type="text/css">'+CSS+'</style></defs></svg>')

    if output_format == "svg":
        response = HttpResponse(content_type='image/svg+xml')
        response['Content-Disposition'] = 'attachment; filename="' + returnFileName + '"'
        response.write(SVG)
    elif output_format == "pdf" or output_format == "png":
        mime_type = "application/x-pdf" if output_format == "pdf" else "image/png"

        response = HttpResponse(content_type=mime_type)
        response['Content-Disposition'] = 'attachment; filename="' + returnFileName + '"'
        iFile = NamedTemporaryFile(delete=False)
        oFile = NamedTemporaryFile(delete=False)
        iFile.write(SVG.encode())
        iFile.close()

        if output_format == "pdf":
            svg2pdf(SVG.encode('utf-8'), write_to=str(oFile.name))
        else:
            svg2png(SVG.encode('utf-8'), write_to=str(oFile.name))

        fileData = oFile.read()
        response.write(fileData)
    else:
        retJSON = {"error": "output format was not recognised"}
        response = JsonResponse(retJSON)
    return response