From 5dd8bcb2f6415171b5bc0a4a7858a0819145494f Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 19 Jul 2016 09:46:56 -0700 Subject: [PATCH] Allow creating annotation arrows w/ default props. `annotate(..., arrowprops={})` now uses an arrow with the default arrow properties, rather than no arrow. Note that this matches the docstring of `annotate`, which indicates that "arrowprops, if not None, is a dictionary of line properties (see matplotlib.lines.Line2D) for the arrow that connects annotation to the point." Comes down to checking for `None` instead of falsiness of `arrowprops`. --- doc/users/whats_new/annotation-default-arrow.rst | 5 +++++ lib/matplotlib/tests/test_axes.py | 10 ++++++++++ lib/matplotlib/text.py | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 doc/users/whats_new/annotation-default-arrow.rst diff --git a/doc/users/whats_new/annotation-default-arrow.rst b/doc/users/whats_new/annotation-default-arrow.rst new file mode 100644 index 00000000000..e885b5b7f71 --- /dev/null +++ b/doc/users/whats_new/annotation-default-arrow.rst @@ -0,0 +1,5 @@ +Annotation can use a default arrow style +---------------------------------------- + +Annotations now use the default arrow style when setting `arrowprops={}`, +rather than no arrow (the new behavior actually matches the documentation). diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index c2f3b5b7c8b..c4e5da5f4e0 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -183,6 +183,16 @@ def test_basic_annotate(): xytext=(3, 3), textcoords='offset points') +@cleanup +def test_annotate_default_arrow(): + # Check that we can make an annotation arrow with only default properties. + fig, ax = plt.subplots() + ann = ax.annotate("foo", (0, 1), xytext=(2, 3)) + assert ann.arrow_patch is None + ann = ax.annotate("foo", (0, 1), xytext=(2, 3), arrowprops={}) + assert ann.arrow_patch is not None + + @image_comparison(baseline_images=['polar_axes']) def test_polar_annotations(): # you can specify the xypoint and the xytext in different diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index c91387d2ace..5cc75f719ab 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -2151,7 +2151,7 @@ def __init__(self, s, xy, self.arrow = None - if arrowprops: + if arrowprops is not None: if "arrowstyle" in arrowprops: arrowprops = self.arrowprops.copy() self._arrow_relpos = arrowprops.pop("relpos", (0.5, 0.5)) @@ -2220,7 +2220,7 @@ def _update_position_xytext(self, renderer, xy_pixel): ox0, oy0 = self._get_xy_display() ox1, oy1 = xy_pixel - if self.arrowprops: + if self.arrowprops is not None: x0, y0 = xy_pixel l, b, w, h = Text.get_window_extent(self, renderer).bounds r = l + w