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

Skip to content

Commit 1639727

Browse files
tonyfasttacaswell
authored andcommitted
Add wai-aria property to the artist accessibility annotations.
1 parent 2828912 commit 1639727

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

lib/matplotlib/artist.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def __init__(self):
192192
self._clippath = None
193193
self._clipon = True
194194
self._label = ''
195+
self._aria = None
195196
self._picker = None
196197
self._rasterized = False
197198
self._agg_filter = None
@@ -1085,6 +1086,35 @@ def set_in_layout(self, in_layout):
10851086
"""
10861087
self._in_layout = in_layout
10871088

1089+
def get_aria(self):
1090+
"""Return any ARIA properties assigned to the artist"""
1091+
return dict(self._aria)
1092+
1093+
def set_aria(self, aria):
1094+
"""
1095+
Set ARIA properties to the artist.
1096+
1097+
A primary use of this method is to attach aria-label to the artist to
1098+
provide alt text to figures.
1099+
1100+
Parameters
1101+
----------
1102+
aria : dict
1103+
1104+
"""
1105+
# TODO validation
1106+
if not isinstance(aria, dict):
1107+
if aria is not None:
1108+
raise TypeError(
1109+
f'aria must be dict or None, not {type(aria)}')
1110+
self._aria = aria
1111+
1112+
def update_aria(self, **aria):
1113+
"""Update WAI-ARIA properties on the artist."""
1114+
# TODO validation
1115+
for k, v in aria.items():
1116+
self._aria[k] = v
1117+
10881118
def get_label(self):
10891119
"""Return the label used for this artist in the legend."""
10901120
return self._label

lib/matplotlib/tests/test_artist.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,10 @@ def draw(self, renderer, extra):
562562

563563
assert 'aardvark' == art.draw(renderer, 'aardvark')
564564
assert 'aardvark' == art.draw(renderer, extra='aardvark')
565+
566+
567+
def test_set_aria():
568+
art = martist.Artist()
569+
with pytest.raises(TypeError, match='^aria must be dict'):
570+
art.set_aria([])
571+
art.set_aria(dict(label="artist alt text"))

0 commit comments

Comments
 (0)