8000 Make signature of Axes.annotate() more explicit. · matplotlib/matplotlib@d6c1d4c · GitHub
[go: up one dir, main page]

Skip to content

Commit d6c1d4c

Browse files
committed
Make signature of Axes.annotate() more explicit.
The signature is identical to Annotation. This makes the parameters explicit instead of `*args`, which improves usability. On the downside, this introduces redundancy. But we can bear that. The signature will not change often and I've added a test that ensures the signatures stay synchronized.
1 parent 447160e commit d6c1d4c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,12 @@ def text(self, x, y, s, fontdict=None, **kwargs):
662662
return t
663663

664664
@docstring.dedent_interpd
665-
def annotate(self, text, xy, *args, **kwargs):
666-
a = mtext.Annotation(text, xy, *args, **kwargs)
665+
def annotate(self, text, xy, xytext=None, xycoords='data', textcoords=None,
666+
arrowprops=None, annotation_clip=None, **kwargs):
667+
# Signature must match Annotation. This is verified in a test.
668+
a = mtext.Annotation(text, xy, xytext=xytext, xycoords=xycoords,
669+
textcoords=textcoords, arrowprops=arrowprops,
670+
annotation_clip=annotation_clip, **kwargs)
667671
a.set_transform(mtransforms.IdentityTransform())
668672
if 'clip_on' in kwargs:
669673
a.set_clip_path(self.patch)

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
from decimal import Decimal
44
from functools import partial
5+
import inspect
56
import io
67
from itertools import product
78
import platform
@@ -26,6 +27,7 @@
2627
import matplotlib.patches as mpatches
2728
import matplotlib.path as mpath
2829
import matplotlib.pyplot as plt
30+
import matplotlib.text as mtext
2931
import matplotlib.ticker as mticker
3032
import matplotlib.transforms as mtransforms
3133
from numpy.testing import (
@@ -622,6 +624,16 @@ def test_annotate_default_arrow():
622624
assert ann.arrow_patch is not None
623625

624626

627+
def test_annotate_signature():
628+
"""Check that the signature of Axes.annotate() matches Annotation."""
629+
fig, ax = plt.subplots()
630+
annotate_params = inspect.signature(ax.annotate).parameters
631+
annotation_params = inspect.signature(mtext.Annotation).parameters
632+
assert list(annotate_params.keys()) == list(annotation_params.keys())
633+
for p1, p2 in zip(annotate_params.values(), annotation_params.values()):
634+
assert p1 == p2
635+
636+
625637
@image_comparison(['fill_units.png'], savefig_kwarg={'dpi': 60})
626638
def test_fill_units():
627639
import matplotlib.testing.jpl_units as units

0 commit c 31C6 omments

Comments
 (0)
0