Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c56027b

Browse filesBrowse files
willemskKherim Willemstimhoffm
authored
[svg] Add rcParam["svg.id"] to add a top-level id attribute to <svg> (#28536)
* (backend.svg) add `svg.id` rcParam If not None, the `svg.id` rcParam value will be used for setting the `id` attribute of the top `<svg>` tag. * Update doc/users/next_whats_new/svg_id_rc.rst * Update doc/users/next_whats_new/svg_id_rc.rst --------- Co-authored-by: Kherim Willems <kherim.willems@imec.be> Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
1 parent 830361d commit c56027b
Copy full SHA for c56027b

File tree

Expand file treeCollapse file tree

5 files changed

+67
-0
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+67
-0
lines changed
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
``svg.id`` rcParam
2+
~~~~~~~~~~~~~~~~~~
3+
:rc:`svg.id` lets you insert an ``id`` attribute into the top-level ``<svg>`` tag.
4+
5+
e.g. ``rcParams["svg.id"] = "svg1"`` results in
6+
default), no ``id`` tag is included
7+
8+
.. code-block:: XML
9+
10+
<svg
11+
xmlns:xlink="http://www.w3.org/1999/xlink"
12+
width="50pt" height="50pt"
13+
viewBox="0 0 50 50"
14+
xmlns="http://www.w3.org/2000/svg"
15+
version="1.1"
16+
id="svg1"
17+
></svg>
18+
19+
This is useful if you would like to link the entire matplotlib SVG file within
20+
another SVG file with the ``<use>`` tag.
21+
22+
.. code-block:: XML
23+
24+
<svg>
25+
<use
26+
width="50" height="50"
27+
xlink:href="mpl.svg#svg1" id="use1"
28+
x="0" y="0"
29+
/></svg>
30+
31+
Where the ``#svg1`` indicator will now refer to the top level ``<svg>`` tag, and
32+
will hence result in the inclusion of the entire file.

‎lib/matplotlib/backends/backend_svg.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_svg.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ def __init__(self, width, height, svgwriter, basename=None, image_dpi=72,
322322
viewBox=f'0 0 {str_width} {str_height}',
323323
xmlns="http://www.w3.org/2000/svg",
324324
version="1.1",
325+
id=mpl.rcParams['svg.id'],
325326
attrib={'xmlns:xlink': "http://www.w3.org/1999/xlink"})
326327
self._write_metadata(metadata)
327328
self._write_default_style()

‎lib/matplotlib/mpl-data/matplotlibrc

Copy file name to clipboardExpand all lines: lib/matplotlib/mpl-data/matplotlibrc
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@
735735
# None: Assume fonts are installed on the
736736
# machine where the SVG will be viewed.
737737
#svg.hashsalt: None # If not None, use this string as hash salt instead of uuid4
738+
#svg.id: None # If not None, use this string as the value for the `id`
739+
# attribute in the top <svg> tag
738740

739741
### pgf parameter
740742
## See https://matplotlib.org/stable/tutorials/text/pgf.html for more information.

‎lib/matplotlib/rcsetup.py

Copy file name to clipboardExpand all lines: lib/matplotlib/rcsetup.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,7 @@ def _convert_validator_spec(key, conv):
13111311
"svg.image_inline": validate_bool,
13121312
"svg.fonttype": ["none", "path"], # save text as text ("none") or "paths"
13131313
"svg.hashsalt": validate_string_or_None,
1314+
"svg.id": validate_string_or_None,
13141315

13151316
# set this when you want to generate hardcopy docstring
13161317
"docstring.hardcopy": validate_bool,

‎lib/matplotlib/tests/test_backend_svg.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backend_svg.py
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,34 @@ def test_annotationbbox_gid():
669669

670670
expected = '<g id="a test for issue 20044">'
671671
assert expected in buf
672+
673+
674+
def test_svgid():
675+
"""Test that `svg.id` rcparam appears in output svg if not None."""
676+
677+
fig, ax = plt.subplots()
678+
ax.plot([1, 2, 3], [3, 2, 1])
679+
fig.canvas.draw()
680+
681+
# Default: svg.id = None
682+
with BytesIO() as fd:
683+
fig.savefig(fd, format='svg')
684+
buf = fd.getvalue().decode()
685+
686+
tree = xml.etree.ElementTree.fromstring(buf)
687+
688+
assert plt.rcParams['svg.id'] is None
689+
assert not tree.findall('.[@id]')
690+
691+
# String: svg.id = str
692+
svg_id = 'a test for issue 28535'
693+
plt.rc('svg', id=svg_id)
694+
695+
with BytesIO() as fd:
696+
fig.savefig(fd, format='svg')
697+
buf = fd.getvalue().decode()
698+
699+
tree = xml.etree.ElementTree.fromstring(buf)
700+
701+
assert plt.rcParams['svg.id'] == svg_id
702+
assert tree.findall(f'.[@id="{svg_id}"]')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.