From 095307f6d3b2348cc4f41dd4e03de06799539511 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 4 Jun 2023 17:34:28 +0200 Subject: [PATCH] Tweak AnnotationBbox coords specification. In AnnotationBbox, it makes sense to allow passing xycoords and boxcoords positionally, because (similarly to annotate()) something like `AnnotationBbox(box, xy1, xy2, "axes transform", "offset points")` (for example) actually reads better than `AnnotationBbox(box, xy1, xy2, xycoords="axes transform", boxcoords="offset points")` once you've seen this often enough and learn that the positional order is just (annotated_xy, box_xy, annotated_coords, box_coords). (The alternative that's fully explicit would be `AnnotationBbox(box, xy=xy1, xybox=xy2, xycoords=..., boxcoords=...)` but even that doesn't read too well because the xy kwargs names don't rhyme exactly with the coords names; or one could reorder the args to `AnnotationBbox(box, xy=xy1, xycoords=..., xybox=xy2, boxcoords=...)`, but that's really a mouthful and doesn't help API usability.) So let's just allow passing the coordinate systems positionally (undoing the change in Matplotlib 3.6 that made them kwonly). The following kwargs do remain kwonly, though. In demo_text_path: xycoords defaults to "data" so doesn't need to be specified; xybox is not specified so boxcoords is not needed. --- .../examples/text_labels_and_annotations/demo_text_path.py | 6 +----- lib/matplotlib/offsetbox.py | 4 +--- lib/matplotlib/offsetbox.pyi | 2 +- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/galleries/examples/text_labels_and_annotations/demo_text_path.py b/galleries/examples/text_labels_and_annotations/demo_text_path.py index 945ff7067ba0..427a4fd69a9f 100644 --- a/galleries/examples/text_labels_and_annotations/demo_text_path.py +++ b/galleries/examples/text_labels_and_annotations/demo_text_path.py @@ -113,11 +113,7 @@ def draw(self, renderer=None): offsetbox.add_artist(text_patch) # place the anchored offset box using AnnotationBbox - ab = AnnotationBbox(offsetbox, (xpos, 0.5), - xycoords='data', - boxcoords="offset points", - box_alignment=(0.5, 0.5), - ) + ab = AnnotationBbox(offsetbox, (xpos, 0.5), box_alignment=(0.5, 0.5)) ax2.add_artist(ab) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index c2c0e54caca0..6b3ed4f42557 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -1224,9 +1224,7 @@ def __str__(self): return f"AnnotationBbox({self.xy[0]:g},{self.xy[1]:g})" @_docstring.dedent_interpd - def __init__(self, offsetbox, xy, xybox=None, *, - xycoords='data', - boxcoords=None, + def __init__(self, offsetbox, xy, xybox=None, xycoords='data', boxcoords=None, *, frameon=True, pad=0.4, # FancyBboxPatch boxstyle. annotation_clip=None, box_alignment=(0.5, 0.5), diff --git a/lib/matplotlib/offsetbox.pyi b/lib/matplotlib/offsetbox.pyi index 3a4c23e9124b..a6027c3a2e8c 100644 --- a/lib/matplotlib/offsetbox.pyi +++ b/lib/matplotlib/offsetbox.pyi @@ -240,7 +240,6 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): offsetbox: OffsetBox, xy: tuple[float, float], xybox: tuple[float, float] | None = ..., - *, xycoords: str | tuple[str, str] | martist.Artist @@ -252,6 +251,7 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): | Transform | Callable[[RendererBase], Bbox | Transform] | None = ..., + *, frameon: bool = ..., pad: float = ..., annotation_clip: bool | None = ...,