In [1]:
%matplotlib inline
from __future__ import division, print_function
from six import StringIO
import xml.etree.ElementTree as etree
import re

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import requests

from svgpath2mpl import parse_path

In [2]:
from IPython.display import Image
Image(url='http://thenewcode.com/assets/images/thumbnails/homer-simpson.svg')


Out[2]:

In [3]:
r = requests.get('http://thenewcode.com/assets/images/thumbnails/homer-simpson.svg')
tree = etree.parse(StringIO(r.text))
root = tree.getroot()
width = int(re.match(r'\d+', root.attrib['width']).group())
height = int(re.match(r'\d+', root.attrib['height']).group())
path_elems = root.findall('.//{http://www.w3.org/2000/svg}path')

In [4]:
paths = [parse_path(elem.attrib['d']) for elem in path_elems]
facecolors = [elem.attrib.get('fill', 'none') for elem in path_elems]
edgecolors = [elem.attrib.get('stroke', 'none') for elem in path_elems]
linewidths = [elem.attrib.get('stroke_width', 1) for elem in path_elems]
collection = mpl.collections.PathCollection(paths, 
                                      edgecolors=edgecolors, 
                                      linewidths=linewidths,
                                      facecolors=facecolors)

In [5]:
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
collection.set_transform(ax.transData)
ax.add_artist(collection)
ax.set_xlim([0, width])
ax.set_ylim([height, 0])


Out[5]:
(500, 0)

In [ ]: