1
  • matplotlib allows generation of svgz files, compression rate mor than 90%
  • external compression of svg files with gzip -9 worse to extremely worse
  • xml.etree.ElementTree allows definition of metadata to svg dataset, but export apparently only to svg, not svgz.

Is there a way in python to add metadata to the xml tree and export it to svgz at the compression rate achieved by matplotlib.pyplot.savefig(..., format='svgz')?

Avión
  • 7,963
  • 11
  • 64
  • 105
D.M.
  • 11
  • 2
  • 1
    You might not be usgin `gzip` correctly, since [`svgz` is `svg` run through `gzip`](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics#Compression). You can even double check this in [`matplotlib`'s implementation](https://github.com/matplotlib/matplotlib/blob/5b74696f838a43bcedc4bc568f0564f87f2fc71a/lib/matplotlib/backends/backend_svg.py#L1206) – Felipe Lema Mar 14 '16 at 16:25
  • Thanks for the link, now I'll try to figure out how the svg backend handles the figures. However my trouble presumably rises from the figure containing several bitmaps with annotations added. Bitmaps within a file don't compress well, while matplotlib or backend_svg seem to handle them in a more convenient way. – D.M. Mar 15 '16 at 08:04

1 Answers1

0

Maybe you would like to do this

import io
from matplotlib.figure import Figure
from matplotlib.backends.backend_svg import FigureCanvasSVG

fig = Figure()
# do your fig stuffs

output = io.BytesIO()
FigureCanvasSVG(fig).print_svg(output)

Now, the SGV can be extracted as fol:

output.getvalue()

If you are looking for SGVZ (gzip compressed) try:

FigureCanvasSVG(fig).print_svgz(output)
Antonio
  • 307
  • 2
  • 11