Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
willemsk opened this issue Jul 10, 2024 · 4 comments
Closed

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

willemsk opened this issue Jul 10, 2024 · 4 comments
Milestone

Comments

@willemsk
Copy link
Contributor

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"})
@willemsk willemsk changed the title [ENH]: [ENH]: Add id attribute to top level svg tag Jul 10, 2024
@tacaswell
Copy link
Member

What do you mean by "consistent"?

@willemsk
Copy link
Contributor Author

willemsk commented Jul 11, 2024

What do you mean by "consistent"?

As in, an id that appears by default in all figures. In the first group, id="figure_1" can be changed using the gid= keyword in plot() I believe, though I haven't tested it yet.

@tacaswell
Copy link
Member

It is changed by setting the gid on the Figure object.

def open_group(self, s, gid=None):
# docstring inherited
if gid:
self.writer.start('g', id=gid)
else:
self._groupd[s] = self._groupd.get(s, 0) + 1
self.writer.start('g', id=f"{s}_{self._groupd[s]:d}")

so when we open a group either we use <group>_<N> or the user supplied gid. Passing it to plot should set the gid on the Line2D created.

@tacaswell
Copy link
Member

This is where we call open_group from the draw method of Figure:

renderer.open_group('figure', gid=self.get_gid())

@tacaswell tacaswell added this to the v3.10.0 milestone Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants