1

I have an image in the .svg format. How do I import it to python and add title and label the axes?

I tried the following:

img = plt.imread('./tune.svg')

But it throws an error. Is there a way to do this?

EngGu
  • 459
  • 3
  • 14

1 Answers1

1

You'll need to read it as an XML file and manipulate its DOM tree. You could either use xml.dom or xml.etree.ElementTree. There are also third-party libraries such as lxml.


Following an example that uses a SVG file from Wikipedia (file version of July 26, 2016) showing the Cantons of Switzerland and changes the background colour of the canton of Zurich leverage xml.etree.ElementTree.

(The path representing the canton of Zurich has an attribute id with the value path2536.)

import xml.etree.ElementTree as ET


OUTPUT_FILE = r"C:\Temp\Switzerland.svg"

# read SVG file
with open("Kantone_der_Schweiz.svg", "r") as file:

    # parse DOM
    svg = ET.parse(file)

    # find Path element of Canton of Zurich (ID: path2536)
    canton_zurich = svg.find(".//*[@id='path2536']")

    # replace style value
    canton_zurich.set("style", "fill:#12e9a1")

    # save updated SVG to file
    with open(OUTPUT_FILE, "wb") as output_file:
        svg.write(output_file, encoding="UTF-8")

Here some useful links:


Below another modified example that does not require downloading the SVG file manually and the modified SVG is plotted with matplotlib (since your question is tagged with matplotlib).

This example requires the following third-party libraries:
cairosvg, matplotlib, and PIL

import io
from urllib.request import Request, urlopen
import xml.etree.ElementTree as ET

import cairosvg
import matplotlib.pyplot as plt
from PIL import Image


SVG_URL = "https://upload.wikimedia.org/wikipedia/commons/8/8b/" \
          "Kantone_der_Schweiz.svg"

request = Request(SVG_URL)

with urlopen(request) as response:

    # read and parse SVG file from URL
    svg = ET.parse(io.BytesIO(response.read()))

    canton_zurich = svg.find(".//*[@id='path2536']")

    canton_zurich.set("style", "fill:#12e9a1")

    # get SVG as a string
    svg_string = ET.tostring(svg.getroot())

    # plot with matplotlib
    # see also https://stackoverflow.com/a/70007704/42659
    png = cairosvg.svg2png(svg_string)
    image = Image.open(io.BytesIO(png))
    plt.imshow(image)
Thomas
  • 8,357
  • 15
  • 45
  • 81