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

Skip to content

Commit c4116bb

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 c4116bb

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,13 @@ 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
668+
# test_annotate_signature().
669+
a = mtext.Annotation(text, xy, xytext=xytext, xycoords=xycoords,
670+
textcoords=textcoords, arrowprops=arrowprops,
671+
annotation_clip=annotation_clip, **kwargs)
667672
a.set_transform(mtransforms.IdentityTransform())
668673
if 'clip_on' in kwargs:
669674
a.set_clip_path(self.patch)

lib/matplotlib/pyplot.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,8 +2334,13 @@ def angle_spectrum(
23342334

23352335
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
23362336
@_copy_docstring_and_deprecators(Axes.annotate)
2337-
def annotate(text, xy, *args, **kwargs):
2338-
return gca().annotate(text, xy, *args, **kwargs)
2337+
def annotate(
2338+
text, xy, xytext=None, xycoords='data', textcoords=None,
2339+
arrowprops=None, annotation_clip=None, **kwargs):
2340+
return gca().annotate(
2341+
text, xy, xytext=xytext, xycoords=xycoords,
2342+
textcoords=textcoords, arrowprops=arrowprops,
2343+
annotation_clip=annotation_clip, **kwargs)
23392344

23402345

23412346
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.

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 comments

Comments
 (0)