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

Skip to content

Commit fbc7472

Browse files
tonyfasttacaswell
authored andcommitted
Add wai-aria property to the artist accessibility annotations.
1 parent 6ed3ab1 commit fbc7472

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/matplotlib/artist.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def __init__(self):
162162
self._clippath = None
163163
self._clipon = True
164164
self._label = ''
165+
self._aria = None
165166
self._picker = None
166167
self._rasterized = False
167168
self._agg_filter = None
@@ -1053,6 +1054,35 @@ def set_in_layout(self, in_layout):
10531054
"""
10541055
self._in_layout = in_layout
10551056

1057+
def get_aria(self):
1058+
"""Return any ARIA properties assigned to the artist"""
1059+
return dict(self._aria)
1060+
1061+
def set_aria(self, aria):
1062+
"""
1063+
Set ARIA properties to the artist.
1064+
1065+
A primary use of this method is to attach aria-label to the artist to
1066+
provide alt text to figures.
1067+
1068+
Parameters
1069+
----------
1070+
aria : dict
1071+
1072+
"""
1073+
# TODO validation
1074+
if not isinstance(aria, dict):
1075+
if aria is not None:
1076+
raise TypeError(
1077+
f'aria must be dict or None, not {type(aria)}')
1078+
self._aria = aria
1079+
1080+
def update_aria(self, **aria):
1081+
"""Update WAI-ARIA properties on the artist."""
1082+
# TODO validation
1083+
for k, v in aria.items():
1084+
self._aria[k] = v
1085+
10561086
def get_label(self):
10571087
"""Return the label used for this artist in the legend."""
10581088
return self._label

lib/matplotlib/tests/test_artist.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,10 @@ def test_format_cursor_data_BoundaryNorm():
535535
assert img.format_cursor_data(v) == label
536536

537537
plt.close()
538+
539+
540+
def test_set_aria():
541+
art = martist.Artist()
542+
with pytest.raises(TypeError, match='^aria must be dict'):
543+
art.set_aria([])
544+
art.set_aria(dict(label="artist alt text"))

0 commit comments

Comments
 (0)