Skip to content

[ENH]: Add id attribute to top level svg tag #28535

Closed
@willemsk

Description

@willemsk

Problem

I would like to link Matplotlib SVGs directly and fully in another SVG using the <use> tag:

<use
  width="50" height="50"
  xlink:href="mpl.svg#svg1" id="use1"
  x="0" y="0"
/>

This allows one to export the linked SVGs as vectors to PDF in Inkscape. However, including a full SVG file requires the presence of an id attribute in the top <svg> tag. Currently, the first tag with a consistent id attribute is the figure_1 group, but using this leads to scaling issues.

Proposed solution

I propose to include a fixed id="svg1" in the top level <svg> tag:

<svg 
  xmlns:xlink="http://www.w3.org/1999/xlink"
  width="50pt" height="50pt"
  viewBox="0 0 50 50"
  xmlns="http://www.w3.org/2000/svg"
  version="1.1"
  id="svg1"
>

Which can be accomplished with a small addition to __init__() of the RendererSVG class:

self._start_id = self.writer.start(
    'svg',
    width=f'{str_width}pt',
    height=f'{str_height}pt',
    viewBox=f'0 0 {str_width} {str_height}',
    xmlns="http://www.w3.org/2000/svg",
    version="1.1",
    id="svg1",
    attrib={'xmlns:xlink': "http://www.w3.org/1999/xlink"})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions