#!/usr/bin/env python # coding: utf-8 # Examples from: # # * https://www.w3.org/TR/SVG/paths.html # * https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d # # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib as mpl import matplotlib.pyplot as plt import svgpath2mpl # In[2]: EXAMPLES = { 'triangle01': { 'width': 400, 'height': 400, 'paths': [{ 'd': "M 100 100 L 300 100 L 200 300 z", 'fill': "red", 'stroke': "blue", 'stroke-width': 3, }], }, 'cubic01': { 'width': 500, 'height': 400, 'paths': [{ 'd': "M100,200 C100,100 250,100 250,200 S400,300 400,200", 'fill': "none", 'stroke': "red", 'stroke-width': 5, }], }, 'quad01': { 'width': 1200, 'height': 600, 'paths': [{ 'd': "M200,300 Q400,50 600,300 T1000,300", 'fill': "none", 'stroke': "red", 'stroke-width': 5, }, { 'd': "M200,300 L400,50 L600,300 L800,550 L1000,300", 'fill': "none", 'stroke': "#888888", 'stroke-width': 2, }], }, 'heart': { 'width': 100, 'height': 100, 'paths': [{ 'd': "M 10,30 " "A 20,20 0,0,1 50,30 " "A 20,20 0,0,1 90,30 " "Q 90,60 50,90 " "Q 10,60 10,30 z", "stroke": "red", 'fill': 'none', 'stroke-width': 6, }], }, 'arcs01': { 'width': 1200, 'height': 400, 'paths': [{ 'd': "M300,200 h-150 a150,150 0 1,0 150,-150 z", 'fill': "red", 'stroke': "blue", 'stroke-width': 5, }, { 'd': "M275,175 v-150 a150,150 0 0,0 -150,150 z", 'fill': "yellow", 'stroke': "blue", 'stroke-width': 5, }, { 'd': "M600,350 l 50,-25 " "a25,25 -30 0,1 50,-25 l 50,-25 " "a25,50 -30 0,1 50,-25 l 50,-25 " "a25,75 -30 0,1 50,-25 l 50,-25 " "a25,100 -30 0,1 50,-25 l 50,-25", 'fill': "none", 'stroke': "red", 'stroke-width': 5, }], }, 'arcs02':{ 'width': 1200, 'height': 400, 'paths': [{ 'd': "M 125,75 a100,50 0 0,0 100,50", 'fill': "none", 'stroke': "red", 'stroke-width': 6, }, { 'd': "M 875,75 a100,50 0 0,1 100,50", 'fill': "none", 'stroke': "red", 'stroke-width': 6, }, { 'd': "M 125,275 a100,50 0 1,0 100,50", 'fill': "none", 'stroke': "red", 'stroke-width': 6, }, { 'd': "M 825,275 a100,50 0 1,1 100,50", 'fill': "none", 'stroke': "red", 'stroke-width': 6, }], } } # In[3]: for name, config in EXAMPLES.items(): fig = plt.figure(figsize=(12, 5.25)) ax = fig.add_subplot(111) for d in config['paths']: path = svgpath2mpl.parse_path(d['d']) patch = mpl.patches.PathPatch( path, facecolor=d.get('fill', 'none'), edgecolor=d.get('stroke', 'black'), linewidth=d.get('stroke-width', 1)) patch.set_transform(ax.transData) ax.add_patch(patch) ax.set_aspect(1) ax.set_xlim([0, config.get('width', 800)]) ax.set_ylim([config.get('height', 800), 0]) plt.title(name) # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: