You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# !pip install cairosvg matplotlib pillowimportiofromurllib.requestimportRequest, urlopenimportxml.etree.ElementTreeasETimportcairosvgimportmatplotlib.pyplotaspltfromPILimportImageSVG_URL="https://upload.wikimedia.org/wikipedia/commons/8/8b/" \
"Kantone_der_Schweiz.svg"request=Request(SVG_URL)
withurlopen(request) asresponse:
# read and parse SVG file from URLsvg=ET.parse(io.BytesIO(response.read()))
canton_zurich=svg.find(".//*[@id='path2536']")
canton_zurich.set("style", "fill:#12e9a1")
# get SVG as a stringsvg_string=ET.tostring(svg.getroot())
# plot with matplotlib# see also https://stackoverflow.com/a/70007704/42659png=cairosvg.svg2png(svg_string)
image=Image.open(io.BytesIO(png))
plt.imshow(image)
Using fsspec
# !pip install cairosvg matplotlib pillow fsspecimportiofromurllib.requestimportRequest, urlopenimportxml.etree.ElementTreeasETimportfsspecimportcairosvgimportmatplotlib.pyplotaspltfromPILimportImageSVG_URL="https://upload.wikimedia.org/wikipedia/commons/8/8b/" \
"Kantone_der_Schweiz.svg"withfsspec.open(SVG_URL) asfile:
# read and parse SVG file from URLsvg=ET.parse(file)
canton_zurich=svg.find(".//*[@id='path2536']")
canton_zurich.set("style", "fill:#12e9a1")
# get SVG as a stringsvg_string=ET.tostring(svg.getroot())
# plot with matplotlib# see also https://stackoverflow.com/a/70007704/42659png=cairosvg.svg2png(svg_string)
image=Image.open(io.BytesIO(png))
plt.imshow(image)