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

Skip to content

Commit 2d28564

Browse files
committed
Add wai-aria property to the artist accessibility annotations.
1 parent 3557392 commit 2d28564

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/matplotlib/artist.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def __init__(self):
161161
self._clippath = None
162162
self._clipon = True
163163
self._label = ''
164+
self._aria = None
164165
self._picker = None
165166
self._rasterized = False
166167
self._agg_filter = None
@@ -1069,6 +1070,33 @@ def update(self, props):
10691070
self.stale = True
10701071
return ret
10711072

1073+
def get_aria(self):
1074+
"""Return any ARIA properties assigned to the artist"""
1075+
return dict(self._aria)
1076+
1077+
def set_aria(self, aria):
1078+
"""
1079+
Set ARIA properties to the artist.
1080+
1081+
A primary use of this method is to attach aria-label to the artist to provide alt text to figures.
1082+
1083+
Parameters
1084+
----------
1085+
aria : dict
1086+
"""
1087+
# TODO validation
1088+
if not isinstance(aria, dict):
1089+
if aria is not None:
1090+
raise TypeError(
1091+
f'aria must be dict or None, not {type(aria)}')
1092+
self._aria = aria
1093+
1094+
def update_aria(self, **aria):
1095+
"""Update WAI-ARIA properties on the artist"""
1096+
# TODO validation
1097+
for k, v in aria.items():
1098+
self._aria[k] = v
1099+
10721100
def get_label(self):
10731101
"""Return the label used for this artist in the legend."""
10741102
return self._label

lib/matplotlib/tests/test_artist.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,9 @@ class MyArtist4(MyArtist3):
374374
pass
375375

376376
assert MyArtist4.set is MyArtist3.set
377+
378+
def test_set_aria():
379+
art = martist.Artist()
380+
with pytest.raises(TypeError, match='^aria must be dict'):
381+
art.set_aria([])
382+
art.set_aria(dict(label="artist alt text"))

0 commit comments

Comments
 (0)