IPython.display: SVG cannot render a svg image generated by svg_filter_line (official example)

Viewed 3967

I am running svg_filter_line (official example) in Jupyter on MacOS with Python3.

SVG works well with lots of svg image (Worldmap image from wiki, for instance)

enter image description here

the original coding of svg_filter_line works well too.

enter image description here

running svg_filter_line generates a svg image named 'svg_filter_line.svg'.

SVG cannot render this svg image 'svg_filter_line.svg', no error, no warning.

enter image description here

1 Answers

It was slightly hard to get to this. Here is the solution: While working with the files: I found that the type of file displayed by display() or display_svg() function is slightly different from each other (Difference is mainly in the namespace). As a workaround, had to re-save the SVG file using svgutils, follow by reading and displaying as below:

import svgutils.transform as sg
from IPython.display import SVG,display

#create new SVG figure
fig = sg.SVGFigure("16cm", "10cm")

# load matpotlib-generated figures
fig1 = sg.fromfile('svg_filter_line.svg')
plot1 = fig1.getroot()
fig.append([plot1])
fig.save("svg_filter_line2.svg")
display(SVG(filename='svg_filter_line2.svg'))

svgutils can be installed as below:

conda install -c conda-forge svgutils

Related