Converts style attributes to classes in plots/svg files generated by matplotlib, and attaches a prefix to ids to make them unique across files.
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
from svgplot import savefig
# Do some plotting
x = np.arange(10)
y = x*x
plt.plot(x, y)
# Set save location (this can also be a string)
image = Path("~/Pictures/image.svg").expanduser()
# Save to file
# id is optional; defaults to 8-character [a-Z0-9] string
savefig(plt, image, id="image")
from svgplot import minify
# Set load location (this can also be a string)
# Image will have the suffix -min added to the name,
# so `image.png` will become `image-min.png`.
image = Path("~/Pictures/image.svg").expanduser()
# Save to file
# id is optional; defaults to 8-character [a-Z0-9] string
minify(image, id="image")
Note that this is not idempotent. Running minify
multiple times on the same file will insert uids multiple times.
There are four steps used inside savefig
and minify
(excluding load/save)
# Convert style-attributes to classes
svg.classify()
# Strip redundant whitespace, e.g. " a b c " -> "a b c"
svg.slim()
# Remove xlink namespace (deprecated in svg2)
svg.svg2()
# Insert id into existing ids, e.g. "#DejaVuSans-1" -> "#DejaVuSans-uid-1"
svg.uid()
This is best seen in the source code in the StyleMap class. E.g. style="stroke-width: 0.8"
is converted to class="thin"
. This is not stable and may be updated to include a prefix to indicate what is thin, e.g. sl-thin
.