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

Skip to content

Commit bd21ebd

Browse files
committed
Fix Annotation using different units and different coords on x/y.
`_get_xy` always needs to be called with self.xycoord, not just the coordinate system of either x or y, so that unitful x/y get correctly converted as needed.
1 parent cf6c569 commit bd21ebd

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import datetime
12
import io
23
import warnings
34

@@ -579,6 +580,18 @@ def test_annotation_update():
579580
rtol=1e-6)
580581

581582

583+
@check_figures_equal(extensions=["png"])
584+
def test_annotation_units(fig_test, fig_ref):
585+
ax = fig_test.add_subplot()
586+
ax.plot(datetime.now(), 1, "o") # Implicitly set axes extents.
587+
ax.annotate("x", (datetime.now(), 0.5), xycoords=("data", "axes fraction"),
588+
# This used to crash before.
589+
xytext=(0, 0), textcoords="offset points")
590+
ax = fig_ref.add_subplot()
591+
ax.plot(datetime.now(), 1, "o")
592+
ax.annotate("x", (datetime.now(), 0.5), xycoords=("data", "axes fraction"))
593+
594+
582595
@image_comparison(['large_subscript_title.png'], style='mpl20')
583596
def test_large_subscript_title():
584597
# Remove this line when this test image is regenerated.

lib/matplotlib/text.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,19 +1864,12 @@ def is_offset(s):
18641864
return isinstance(s, str) and s.split()[0] == "offset"
18651865

18661866
if isinstance(self.xycoords, tuple):
1867-
s1, s2 = self.xycoords
1868-
if is_offset(s1) or is_offset(s2):
1867+
if any(map(is_offset, self.xycoords)):
18691868
raise ValueError("xycoords should not be an offset coordinate")
1870-
x, y = self.xy
1871-
x1, y1 = self._get_xy(renderer, x, y, s1)
1872-
x2, y2 = self._get_xy(renderer, x, y, s2)
1873-
return x1, y2
18741869
elif is_offset(self.xycoords):
18751870
raise ValueError("xycoords should not be an offset coordinate")
1876-
else:
1877-
x, y = self.xy
1878-
return self._get_xy(renderer, x, y, self.xycoords)
1879-
#raise RuntimeError("must be defined by the derived class")
1871+
x, y = self.xy
1872+
return self._get_xy(renderer, x, y, self.xycoords)
18801873

18811874
# def _get_bbox(self, renderer):
18821875
# if hasattr(bbox, "bounds"):

0 commit comments

Comments
 (0)