svg in plot

Hi everybody,
Is there a way to import a svg-image into a plot?
I know that there are some possibilities to import png (http://matplotlib.sourceforge.net/examples/pylab_examples/demo_annotation_box.html) or eps (which is then rastered).
But till now I didn't find any way to import/embed a real vector graphic which is still a vector when I save the figure again as pdf or svg.
Thanks in advance,
HoWil

···

--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

Hi William,

I am fairly certain that matplotlib does not have the capability to do what you are looking for. (If I am wrong, I'm sure someone will correct me.)

You may have better luck using something like Scribus or Inkscape.

Best,
Jeff

···

On Feb 16, 2012, at 2:43 PM, William Hoburg wrote:

Hi everybody,
Is there a way to import a svg-image into a plot?
I know that there are some possibilities to import png (http://matplotlib.sourceforge.net/examples/pylab_examples/demo_annotation_box.html) or eps (which is then rastered).
But till now I didn't find any way to import/embed a real vector graphic which is still a vector when I save the figure again as pdf or svg.
Thanks in advance,
HoWil
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

William,

This is correct. Matplotlib currently has no import feature available. Along these lines has been several requests for an ability to import Matlab .fig files as well. I believe the official position is that matplotlib is primarily an exporter library, not an importer library. The exception to this rule appears to be imread()…

There is absolutely no reason why a module could not be made for this, given that everything in matplotlib is assumed to be vector-based. You just need a library that can load up the data in the SVG file into information that is sensibly organized. Then you run through that data, producing the relevant artists and collections, adding them to the axes object.

Maybe pySVG might be a good start?

Cheers!
Ben Root

···

On Thu, Feb 16, 2012 at 1:53 PM, Jeffrey Blackburne <jblackburne@…3027…> wrote:

Hi William,

I am fairly certain that matplotlib does not have the capability to

do what you are looking for. (If I am wrong, I’m sure someone will

correct me.)

You may have better luck using something like Scribus or Inkscape.

Best,

Jeff

William Hoburg:

Is there a way to import a svg-image into a plot?
I know that there are some possibilities to import png (http://matplotlib.sourceforge.net/examples/pylab_examples/demo_annotation_box.html) or eps (which is then rastered).
But till now I didn't find any way to import/embed a real vector graphic which is still a vector when I save the figure again as pdf or svg.

SVG is a text file, a quite complicated XML. In order to put it into a canvas you have to parse it, and to transform all the DEFs, the primitives and attributes into plotting commands. Such package as matplotlib lives in anther galaxy. It took a lot of time to implement SVG in Mozilla or Chrome...

Jerzy Karczmarczuk

I forgot to add something...

Benjamin Root :

There is absolutely no reason why a module could not be made for this, given that everything in matplotlib is assumed to be vector-based. You just need a library that can load up the data in the SVG file into information that is sensibly organized.

In principle a decent parser can be added to Matplotlib. But...

SVG is NOT entirely a vector drawing program!!

1. You have gradients, clipping paths, patterns, and filtering, which interpolates between vector and raster data. You will not implement easily as an "artist" the blur, displacement maps, or morphologic filters

2. SMIL style animation needs a specific engine, this will not easily work on a back-end independent framework.

···

==

Usually I hate people who discourage others, or say that something cannot be done. But here, such a module would take too long to implement, and the gain seems not adequate. I would be VERY HAPPY, if I am wrong.

Jerzy K.

I forgot to add something…

Benjamin Root :

There is absolutely no reason why a module could not be made for this,

given that everything in matplotlib is assumed to be vector-based.

You just need a library that can load up the data in the SVG file into

information that is sensibly organized.

In principle a decent parser can be added to Matplotlib. But…

SVG is NOT entirely a vector drawing program!!

  1. You have gradients, clipping paths, patterns, and filtering, which

interpolates between vector and raster data. You will not implement

easily as an “artist” the blur, displacement maps, or morphologic filters

matplotlib does have AGG filters, which are very powerful. I do concede that not everything in the SVG spec can be done in matplotlib, but you would be surprised what can be done.

  1. SMIL style animation needs a specific engine, this will not easily

work on a back-end independent framework.

I believe the context of the question isn’t for animations (although we do have a backend-independent framework for them, too), but for static SVGs. I don’t think anybody is suggesting a complete solution here. A module that can load up many of the common components of an SVG file into a list of artist and collection objects would be neat, even if it has to throw out lots of data in an SVG file.

Of course, because it would be impossible to fully implement, such a module would never be included in matplotlib, but that shouldn’t stop someone from creating a useful basic tool.

Cheers!
Ben Root

···

On Thu, Feb 16, 2012 at 2:41 PM, Jerzy Karczmarczuk <jerzy.karczmarczuk@…3937…> wrote:

Benjamin Root :

it would be impossible to fully implement, such a module would never be included in matplotlib, but that shouldn't stop someone from creating a useful basic tool.

Yes. Thank you Ben. I so concentrated on the "vector" side of the original question that I forgot that AGG has a rasterizer and filters, and morover Matplotlib can rescale (regenerate) bitmaps when resizing the figure!

OK. Since the ActiveState cookbook
http://code.activestate.com/recipes/325823-draw-svg-images-in-python/
offers some programme to draw SVGs, and the SVG parsers are doable, somebody might start tomorrow.
Great!

Jerzy K.

Hi,

if the import filetype is not restricted to svg, you can use PyX to place vector eps figures into other eps figures. I used this in a two step approach (first create the plot with matplotlib and store is as eps and second, use PyX to combine the plot with a vector figure) in a single script.

This usually worked fine and the resulting file is still completely vectorized!

br
Jakob

···

On 02/16/2012 08:43 PM, William Hoburg wrote:

Hi everybody,
Is there a way to import a svg-image into a plot?
I know that there are some possibilities to import png (http://matplotlib.sourceforge.net/examples/pylab_examples/demo_annotation_box.html) or eps (which is then rastered).
But till now I didn't find any way to import/embed a real vector graphic which is still a vector when I save the figure again as pdf or svg.
Thanks in advance,
HoWil

Hi all,

I had the same problem and I implemented a small library for manipulating SVG figures (for example, matplotlib-generated). It is on github:

https://github.com/btel/svg_utils

There is also a related blog post:

http://neuroscience.telenczuk.pl/?p=331

The library does NOT allow to import svg figures into matplotlib, but it allows to combine a mpl figure exported to SVG and and a external SVG file.

I hope it helps!

Cheers,

Bartosz

···

On 16.02.2012, at 20:43, William Hoburg wrote:

Hi everybody,
Is there a way to import a svg-image into a plot?
I know that there are some possibilities to import png (http://matplotlib.sourceforge.net/examples/pylab_examples/demo_annotation_box.html) or eps (which is then rastered).
But till now I didn't find any way to import/embed a real vector graphic which is still a vector when I save the figure again as pdf or svg.
Thanks in advance,
HoWil
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Bartosz Telenczuk

Institute for Theoretical Biology
Humboldt University of Berlin, Germany
Phone: +4930/2093-8838
Homepage: http://neuroscience.telenczuk.pl

Hi

@Jakob and Bartosz

Thx; Both approches (PYX and svgutils) worked well.
But there are some things to consider. svgutils for instance uses coordinates from the top left corner.
On Ubuntu (I use 11.10 x64) PYX can be installed from repository using synaptics. svgutils can be installed without problems from the github repository.

I included (as text to avoid download problems for others) a example script where I utilize mpl to generate svg and eps files and combine the eps using PYX and the svg using svgutils.

@ Jerzy Karczmarczuk:

1. You have gradients, clipping paths, patterns, and filtering, which
interpolates between vector and raster data. You will not implement
easily as an "artist" the blur, displacement maps, or morphologic filters

What I intended to do can be seen in the attached svg figure. I do not need gradient, filtering, effects and so on. I only need basic functionality like lines or circles do add simple graphics into my mpl plot. The subfigure inside the mpl plot was drawn in Inkscape now inserted svgutils. The resulting svg (or eps) will be used in my lyx document which is converted to a pdf on the fly by Inkscape.

@ all: Did anyone try http://cairographics.org/pyrsvg/ to achieve the above questioned.

Cheers,
HoWil

···

#==============================================================================
#==============================================================================
# Demo script to combine two vector graphics like eps and svg in python.
# The combined files in this demo were generated using matplotlib

from matplotlib import *
from matplotlib.pyplot import *

#==============================================================================
# Build a figure and export it as eps and svg in two different versions
#==============================================================================

# Figure adjustment
f_Xsize_cm = 10.;
f_Ysize_cm = 8.;

fig = figure(figsize=(f_Xsize_cm/2.5, f_Ysize_cm/2.5));

fig.patch.set_alpha(0.2)

#
ax = fig.add_subplot(111)
ax.patch.set_alpha(0.7)

text(0.22,0.25,'eps-SVG \n vector \n graphics',fontsize=30, color=[0.6,0.6,0.6])
# Fill the axis with some color
ax.set_axis_bgcolor([0.9,0.9,0.1])

# Remove all ticks and ticklabels to get rectangles
ax.set_yticklabels(())
ax.set_xticklabels(())
ax.set_xticks([])
ax.set_yticks([])

# Export first version of eps and svg figure
fig.savefig('exportSub.eps');
fig.savefig('exportSub.svg');

# Export second version of eps and svg figure
ax.set_axis_bgcolor([0.1,0.9,0.1])
text(0.1,0.4,'2nd',fontsize=40, color=[0.9,0.2,0.2], rotation=45)
fig.savefig('exportSub2nd.eps');
fig.savefig('exportSub2nd.svg');

#==============================================================================
# Utilizing 'Pyx' from http://pyx.sourceforge.net/
#==============================================================================
from pyx import *

c = canvas.canvas()
# load both external files
c.insert(epsfile.epsfile(0, 0, "exportSub.eps"))
c.insert(epsfile.epsfile(f_Xsize_cm/2, f_Ysize_cm/3, "exportSub2nd.eps"))

# save generated EPS files
c.writeEPSfile("export")

#==============================================================================
# Utilizing 'svgutils' from https://github.com/btel/svg_utils
#==============================================================================
import svgutils.transform as sg
#import sys

#create new SVG figure
fig = sg.SVGFigure()

# load both external files
fig1 = sg.fromfile('exportSub.svg')
fig2 = sg.fromfile('exportSub2nd.svg')

# get the plot objects
plot1 = fig1.getroot()
# mpl-figures have to be scaled
plot1.moveto(0, 0, scale=1.25)

plot2 = fig2.getroot()
# x-,y- coordinates are measured from the top left corner
factor = 35.433 # scale factor cm/px, out of inkscape
plot2.moveto(f_Xsize_cm/2 * factor, -f_Ysize_cm/3 * factor, scale=1.25)

# append plots and labels to figure
fig.append([plot1])
fig.append([plot2])

# save generated SVG files
fig.save("export.svg")

--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

Hi,

It is a very nice example. svgutils is a svg editting library and so it does not do any transformations itself. All the coordinates must be defined in the SVG space, whose origin is in the top left.

Cheers,

Bartosz

···

On 21.02.2012, at 09:33, William Hoburg wrote:

Hi

@Jakob and Bartosz

Thx; Both approches (PYX and svgutils) worked well.
But there are some things to consider. svgutils for instance uses coordinates from the top left corner.
On Ubuntu (I use 11.10 x64) PYX can be installed from repository using synaptics. svgutils can be installed without problems from the github repository.

I included (as text to avoid download problems for others) a example script where I utilize mpl to generate svg and eps files and combine the eps using PYX and the svg using svgutils.

@ Jerzy Karczmarczuk:

1. You have gradients, clipping paths, patterns, and filtering, which
interpolates between vector and raster data. You will not implement
easily as an "artist" the blur, displacement maps, or morphologic filters

What I intended to do can be seen in the attached svg figure. I do not need gradient, filtering, effects and so on. I only need basic functionality like lines or circles do add simple graphics into my mpl plot. The subfigure inside the mpl plot was drawn in Inkscape now inserted svgutils. The resulting svg (or eps) will be used in my lyx document which is converted to a pdf on the fly by Inkscape.

@ all: Did anyone try http://cairographics.org/pyrsvg/ to achieve the above questioned.

Cheers,
HoWil

#==============================================================================
#==============================================================================
# Demo script to combine two vector graphics like eps and svg in python.
# The combined files in this demo were generated using matplotlib

from matplotlib import *
from matplotlib.pyplot import *

#==============================================================================
# Build a figure and export it as eps and svg in two different versions
#==============================================================================

# Figure adjustment
f_Xsize_cm = 10.;
f_Ysize_cm = 8.;

fig = figure(figsize=(f_Xsize_cm/2.5, f_Ysize_cm/2.5));

fig.patch.set_alpha(0.2)

#
ax = fig.add_subplot(111)
ax.patch.set_alpha(0.7)

text(0.22,0.25,'eps-SVG \n vector \n graphics',fontsize=30, color=[0.6,0.6,0.6])
# Fill the axis with some color
ax.set_axis_bgcolor([0.9,0.9,0.1])

# Remove all ticks and ticklabels to get rectangles
ax.set_yticklabels(())
ax.set_xticklabels(())
ax.set_xticks([])
ax.set_yticks([])

# Export first version of eps and svg figure
fig.savefig('exportSub.eps');
fig.savefig('exportSub.svg');

# Export second version of eps and svg figure
ax.set_axis_bgcolor([0.1,0.9,0.1])
text(0.1,0.4,'2nd',fontsize=40, color=[0.9,0.2,0.2], rotation=45)
fig.savefig('exportSub2nd.eps');
fig.savefig('exportSub2nd.svg');

#==============================================================================
# Utilizing 'Pyx' from http://pyx.sourceforge.net/
#==============================================================================
from pyx import *

c = canvas.canvas()
# load both external files
c.insert(epsfile.epsfile(0, 0, "exportSub.eps"))
c.insert(epsfile.epsfile(f_Xsize_cm/2, f_Ysize_cm/3, "exportSub2nd.eps"))

# save generated EPS files
c.writeEPSfile("export")

#==============================================================================
# Utilizing 'svgutils' from https://github.com/btel/svg_utils
#==============================================================================
import svgutils.transform as sg
#import sys

#create new SVG figure
fig = sg.SVGFigure()

# load both external files
fig1 = sg.fromfile('exportSub.svg')
fig2 = sg.fromfile('exportSub2nd.svg')

# get the plot objects
plot1 = fig1.getroot()
# mpl-figures have to be scaled
plot1.moveto(0, 0, scale=1.25)

plot2 = fig2.getroot()
# x-,y- coordinates are measured from the top left corner
factor = 35.433 # scale factor cm/px, out of inkscape
plot2.moveto(f_Xsize_cm/2 * factor, -f_Ysize_cm/3 * factor, scale=1.25)

# append plots and labels to figure
fig.append([plot1])
fig.append([plot2])

# save generated SVG files
fig.save("export.svg")

--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

Bartosz Telenczuk

Institute for Theoretical Biology
Humboldt University of Berlin, Germany
Phone: +4930/2093-8838
Homepage: http://neuroscience.telenczuk.pl