Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[svg] Add rcParam["svg.id"] to add a top-level id attribute to <svg> #28536

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

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions doc/users/next_whats_new/svg_id_rc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
``svg.id`` rcParam
~~~~~~~~~~~~~~~~~~
:rc:`svg.id` lets you insert an ``id`` attribute into the top-level ``<svg>`` tag.

e.g. ``rcParams["svg.id"] = "svg1"`` results in
default), no ``id`` tag is included

.. code-block:: XML

<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"
></svg>

This is useful if you would like to link the entire matplotlib SVG file within
another SVG file with the ``<use>`` tag.

.. code-block:: XML

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

Where the ``#svg1`` indicator will now refer to the top level ``<svg>`` tag, and
will hence result in the inclusion of the entire file.
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def __init__(self, width, height, svgwriter, basename=None, image_dpi=72,
viewBox=f'0 0 {str_width} {str_height}',
xmlns="http://www.w3.org/2000/svg",
version="1.1",
id=mpl.rcParams['svg.id'],
attrib={'xmlns:xlink': "http://www.w3.org/1999/xlink"})
self._write_metadata(metadata)
self._write_default_style()
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,8 @@
# None: Assume fonts are installed on the
# machine where the SVG will be viewed.
#svg.hashsalt: None # If not None, use this string as hash salt instead of uuid4
#svg.id: None # If not None, use this string as the value for the `id`
# attribute in the top <svg> tag

### pgf parameter
## See https://matplotlib.org/stable/tutorials/text/pgf.html for more information.
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,7 @@ def _convert_validator_spec(key, conv):
"svg.image_inline": validate_bool,
"svg.fonttype": ["none", "path"], # save text as text ("none") or "paths"
"svg.hashsalt": validate_string_or_None,
"svg.id": validate_string_or_None,

# set this when you want to generate hardcopy docstring
"docstring.hardcopy": validate_bool,
Expand Down
31 changes: 31 additions & 0 deletions lib/matplotlib/tests/test_backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,34 @@ def test_annotationbbox_gid():

expected = '<g id="a test for issue 20044">'
assert expected in buf


def test_svgid():
"""Test that `svg.id` rcparam appears in output svg if not None."""

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [3, 2, 1])
fig.canvas.draw()

# Default: svg.id = None
with BytesIO() as fd:
fig.savefig(fd, format='svg')
buf = fd.getvalue().decode()

tree = xml.etree.ElementTree.fromstring(buf)

assert plt.rcParams['svg.id'] is None
assert not tree.findall('.[@id]')

# String: svg.id = str
svg_id = 'a test for issue 28535'
plt.rc('svg', id=svg_id)

with BytesIO() as fd:
fig.savefig(fd, format='svg')
buf = fd.getvalue().decode()

tree = xml.etree.ElementTree.fromstring(buf)

assert plt.rcParams['svg.id'] == svg_id
assert tree.findall(f'.[@id="{svg_id}"]')
Loading