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

Skip to content

Commit 4b25853

Browse files
committed
Merge pull request #4843 from tacaswell/REV_coord_wrapping
Rev coord wrapping
2 parents d69c7ac + 2a23b7c commit 4b25853

File tree

6 files changed

+68
-180
lines changed

6 files changed

+68
-180
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
No annotation coordinates wrap
2+
``````````````````````````````
3+
4+
In #2351 for 1.4.0 the behavior of ['axes points', 'axes pixel',
5+
'figure points', 'figure pixel'] as coordinates was change to
6+
no longer wrap for negative values. In 1.4.3 this change was
7+
reverted for 'axes points' and 'axes pixel' and in addition caused
8+
'axes fraction' to wrap. For 1.5 the behavior has been reverted to
9+
as it was in 1.4.0-1.4.2, no wrapping for any type of coordinate.
Binary file not shown.

lib/matplotlib/tests/test_text.py

Lines changed: 53 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -313,169 +313,59 @@ def test_bbox_clipping():
313313
t.set_bbox({"boxstyle": "round, pad=0.1"})
314314

315315

316-
@image_comparison(baseline_images=['annotation_negative_coords'],
316+
@image_comparison(baseline_images=['annotation_negative_ax_coords'],
317317
extensions=['png'])
318-
def test_annotation_negative_coords():
319-
fig = plt.figure()
320-
ax = plt.subplot(1, 1, 1)
321-
322-
ax.annotate("+fpt", (15, 40), xycoords="figure points")
323-
ax.annotate("+fpx", (25, 30), xycoords="figure pixels")
324-
ax.annotate("+apt", (35, 20), xycoords="axes points")
325-
ax.annotate("+apx", (45, 10), xycoords="axes pixels")
326-
327-
ax.annotate("-fpt", (-55, -40), xycoords="figure points")
328-
ax.annotate("-fpx", (-45, -30), xycoords="figure pixels")
329-
ax.annotate("-apt", (-35, -20), xycoords="axes points")
330-
ax.annotate("-apx", (-25, -10), xycoords="axes pixels")
331-
332-
333-
@cleanup
334-
def test_text_annotation_get_window_extent():
335-
figure = Figure(dpi=100)
336-
renderer = RendererAgg(200, 200, 100)
337-
338-
# Only text annotation
339-
annotation = Annotation('test', xy=(0, 0))
340-
annotation.set_figure(figure)
341-
342-
text = Text(text='test', x=0, y=0)
343-
text.set_figure(figure)
344-
345-
bbox = annotation.get_window_extent(renderer=renderer)
346-
347-
text_bbox = text.get_window_extent(renderer=renderer)
348-
eq_(bbox.width, text_bbox.width)
349-
eq_(bbox.height, text_bbox.height)
350-
351-
_, _, d = renderer.get_text_width_height_descent(
352-
'text', annotation._fontproperties, ismath=False)
353-
_, _, lp_d = renderer.get_text_width_height_descent(
354-
'lp', annotation._fontproperties, ismath=False)
355-
below_line = max(d, lp_d)
356-
357-
# These numbers are specific to the current implementation of Text
358-
points = bbox.get_points()
359-
eq_(points[0, 0], 0.0)
360-
eq_(points[1, 0], text_bbox.width)
361-
eq_(points[0, 1], -below_line)
362-
eq_(points[1, 1], text_bbox.height - below_line)
363-
364-
365-
@cleanup
366-
def test_text_with_arrow_annotation_get_window_extent():
367-
headwidth = 21
368-
fig, ax = plt.subplots(dpi=100)
369-
txt = ax.text(s='test', x=0, y=0)
370-
ann = ax.annotate(
371-
'test',
372-
xy=(0.0, 50.0),
373-
xytext=(50.0, 50.0), xycoords='figure pixels',
374-
arrowprops={
375-
'facecolor': 'black', 'width': 2,
376-
'headwidth': headwidth, 'shrink': 0.0})
377-
378-
plt.draw()
379-
renderer = fig.canvas.renderer
380-
# bounding box of text
381-
text_bbox = txt.get_window_extent(renderer=renderer)
382-
# bounding box of annotation (text + arrow)
383-
bbox = ann.get_window_extent(renderer=renderer)
384-
# bounding box of arrow
385-
arrow_bbox = ann.arrow_patch.get_window_extent(renderer)
386-
# bounding box of annotation text
387-
ann_txt_bbox = Text.get_window_extent(ann)
388-
389-
# make sure annotation width is 50 px wider than
390-
# just the text
391-
eq_(bbox.width, text_bbox.width + 50.0)
392-
# make sure the annotation text bounding box is same size
393-
# as the bounding box of the same string as a Text object
394-
eq_(ann_txt_bbox.height, text_bbox.height)
395-
eq_(ann_txt_bbox.width, text_bbox.width)
396-
# compute the expected bounding box of arrow + text
397-
expected_bbox = Bbox.union([ann_txt_bbox, arrow_bbox])
398-
assert_almost_equal(bbox.height, expected_bbox.height)
399-
400-
401-
@cleanup
402-
def test_arrow_annotation_get_window_extent():
403-
dpi = 100
404-
dots_per_point = dpi / 72
405-
figure = Figure(dpi=dpi)
406-
figure.set_figwidth(2.0)
407-
figure.set_figheight(2.0)
408-
renderer = RendererAgg(200, 200, 100)
409-
410-
# Text annotation with arrow; arrow dimensions are in points
411-
annotation = Annotation(
412-
'', xy=(0.0, 50.0), xytext=(50.0, 50.0), xycoords='figure pixels',
413-
arrowprops={
414-
'facecolor': 'black', 'width': 8, 'headwidth': 10, 'shrink': 0.0})
415-
annotation.set_figure(figure)
416-
annotation.draw(renderer)
417-
418-
bbox = annotation.get_window_extent()
419-
points = bbox.get_points()
420-
421-
eq_(bbox.width, 50.0)
422-
assert_almost_equal(bbox.height, 10.0 * dots_per_point)
423-
eq_(points[0, 0], 0.0)
424-
eq_(points[0, 1], 50.0 - 5 * dots_per_point)
425-
426-
427-
@cleanup
428-
def test_empty_annotation_get_window_extent():
429-
figure = Figure(dpi=100)
430-
figure.set_figwidth(2.0)
431-
figure.set_figheight(2.0)
432-
renderer = RendererAgg(200, 200, 100)
433-
434-
# Text annotation with arrow
435-
annotation = Annotation(
436-
'', xy=(0.0, 50.0), xytext=(0.0, 50.0), xycoords='figure pixels')
437-
annotation.set_figure(figure)
438-
annotation.draw(renderer)
439-
440-
bbox = annotation.get_window_extent()
441-
points = bbox.get_points()
442-
443-
eq_(points[0, 0], 0.0)
444-
eq_(points[1, 0], 0.0)
445-
eq_(points[1, 1], 50.0)
446-
eq_(points[0, 1], 50.0)
447-
318+
def test_annotation_negative_ax_coords():
319+
fig, ax = plt.subplots()
448320

449-
@image_comparison(baseline_images=['basictext_wrap'],
450-
extensions=['png'])
451-
def test_basic_wrap():
452-
fig = plt.figure()
453-
plt.axis([0, 10, 0, 10])
454-
t = "This is a really long string that I'd rather have wrapped so that" \
455-
" it doesn't go outside of the figure, but if it's long enough it" \
456-
" will go off the top or bottom!"
457-
plt.text(4, 1, t, ha='left', rotation=15, wrap=True)
458-
plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
459-
plt.text(5, 5, t, ha='right', rotation=-15, wrap=True)
460-
plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',
461-
va='top', wrap=True)
462-
plt.text(3, 4, t, family='serif', style='italic', ha='right', wrap=True)
463-
plt.text(-1, 0, t, ha='left', rotation=-15, wrap=True)
464-
465-
466-
@image_comparison(baseline_images=['fonttext_wrap'],
321+
ax.annotate('+ pts',
322+
xytext=[30, 20], textcoords='axes points',
323+
xy=[30, 20], xycoords='axes points', fontsize=32)
324+
ax.annotate('- pts',
325+
xytext=[30, -20], textcoords='axes points',
326+
xy=[30, -20], xycoords='axes points', fontsize=32,
327+
va='top')
328+
ax.annotate('+ frac',
329+
xytext=[0.75, 0.05], textcoords='axes fraction',
330+
xy=[0.75, 0.05], xycoords='axes fraction', fontsize=32)
331+
ax.annotate('- frac',
332+
xytext=[0.75, -0.05], textcoords='axes fraction',
333+
xy=[0.75, -0.05], xycoords='axes fraction', fontsize=32,
334+
va='top')
335+
336+
ax.annotate('+ pixels',
337+
xytext=[160, 25], textcoords='axes pixels',
338+
xy=[160, 25], xycoords='axes pixels', fontsize=32)
339+
ax.annotate('- pixels',
340+
xytext=[160, -25], textcoords='axes pixels',
341+
xy=[160, -25], xycoords='axes pixels', fontsize=32,
342+
va='top')
343+
344+
345+
@image_comparison(baseline_images=['annotation_negative_fig_coords'],
467346
extensions=['png'])
468-
def test_font_wrap():
469-
fig = plt.figure()
470-
plt.axis([0, 10, 0, 10])
471-
t = "This is a really long string that I'd rather have wrapped so that" \
472-
" it doesn't go outside of the figure, but if it's long enough it" \
473-
" will go off the top or bottom!"
474-
plt.text(4, -1, t, fontsize=18, family='serif', ha='left', rotation=15,
475-
wrap=True)
476-
plt.text(6, 5, t, family='sans serif', ha='left', rotation=15, wrap=True)
477-
plt.text(5, 5, t, weight='light', ha='right', rotation=-15, wrap=True)
478-
plt.text(5, 10, t, weight='heavy', ha='center', va='top', wrap=True)
479-
plt.text(3, 4, t, family='monospace', ha='right', wrap=True)
480-
plt.text(-1, 0, t, fontsize=14, style='italic', ha='left', rotation=-15,
481-
wrap=True)
347+
def test_annotation_negative_fig_coords():
348+
fig, ax = plt.subplots()
349+
350+
ax.annotate('+ pts',
351+
xytext=[10, 120], textcoords='figure points',
352+
xy=[10, 120], xycoords='figure points', fontsize=32)
353+
ax.annotate('- pts',
354+
xytext=[-10, 180], textcoords='figure points',
355+
xy=[-10, 180], xycoords='figure points', fontsize=32,
356+
va='top')
357+
ax.annotate('+ frac',
358+
xytext=[0.05, 0.55], textcoords='figure fraction',
359+
xy=[0.05, 0.55], xycoords='figure fraction', fontsize=32)
360+
ax.annotate('- frac',
361+
xytext=[-0.05, 0.5], textcoords='figure fraction',
362+
xy=[-0.05, 0.5], xycoords='figure fraction', fontsize=32,
363+
va='top')
364+
365+
ax.annotate('+ pixels',
366+
xytext=[50, 50], textcoords='figure pixels',
367+
xy=[50, 50], xycoords='figure pixels', fontsize=32)
368+
ax.annotate('- pixels',
369+
xytext=[-50, 100], textcoords='figure pixels',
370+
xy=[-50, 100], xycoords='figure pixels', fontsize=32,
371+
va='top')

lib/matplotlib/text.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,17 +1704,17 @@ def _get_xy(self, renderer, x, y, s):
17041704
if s2 == 'data':
17051705
y = float(self.convert_yunits(y))
17061706

1707-
tr = self._get_xy_transform(renderer, (x, y), s)
1707+
tr = self._get_xy_transform(renderer, s)
17081708
x1, y1 = tr.transform_point((x, y))
17091709
return x1, y1
17101710

1711-
def _get_xy_transform(self, renderer, xy, s):
1711+
def _get_xy_transform(self, renderer, s):
17121712

17131713
if isinstance(s, tuple):
17141714
s1, s2 = s
17151715
from matplotlib.transforms import blended_transform_factory
1716-
tr1 = self._get_xy_transform(renderer, xy, s1)
1717-
tr2 = self._get_xy_transform(renderer, xy, s2)
1716+
tr1 = self._get_xy_transform(renderer, s1)
1717+
tr2 = self._get_xy_transform(renderer, s2)
17181718
tr = blended_transform_factory(tr1, tr2)
17191719
return tr
17201720

@@ -1763,17 +1763,7 @@ def _get_xy_transform(self, renderer, xy, s):
17631763
# bbox0 = self._get_bbox(renderer, bbox)
17641764

17651765
if bbox0 is not None:
1766-
x, y = xy
1767-
bounds = bbox0.extents
1768-
if x < 0:
1769-
x0 = bounds[2]
1770-
else:
1771-
x0 = bounds[0]
1772-
if y < 0:
1773-
y0 = bounds[3]
1774-
else:
1775-
y0 = bounds[1]
1776-
xy0 = (x0, y0)
1766+
xy0 = bbox0.bounds[:2]
17771767
elif bbox_name == "offset":
17781768
xy0 = self._get_ref_xy(renderer)
17791769

@@ -2120,8 +2110,7 @@ def _update_position_xytext(self, renderer, xy_pixel):
21202110
patch.
21212111
"""
21222112
# generate transformation,
2123-
self.set_transform(self._get_xy_transform(
2124-
renderer, self.xy, self.anncoords))
2113+
self.set_transform(self._get_xy_transform(renderer, self.anncoords))
21252114

21262115
ox0, oy0 = self._get_xy_display()
21272116
ox1, oy1 = xy_pixel

0 commit comments

Comments
 (0)