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

Skip to content

Commit 07d955e

Browse files
committed
Replace ClabelText by set_transform_rotates_text.
ContourLabeler previously optionally used a custom Text subclass to ensure that text rotation took place in data space, not in screen space, but this is now available for all Text instances via the transform_rotates_text property, so directly use that. This means that _get_label_text, _add_label, and set_label_props can also directly get inlined into their now only callsite (add_label).
1 parent 6cc7cda commit 07d955e

File tree

2 files changed

+31
-47
lines changed

2 files changed

+31
-47
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
``contour.ClabelText`` and ``ContourLabeler.set_label_props``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... are deprecated.
4+
5+
Use ``Text(..., transform_rotates_text=True)`` as a replacement for
6+
``contour.ClabelText(...)`` and ``text.set(text=text, color=color,
7+
fontproperties=labeler.labelFontProps, clip_box=labeler.axes.bbox)`` as a
8+
replacement for the ``ContourLabeler.set_label_props(label, text, color)``.

lib/matplotlib/contour.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# per level.
3232

3333

34+
@_api.deprecated("3.7", alternative="Text.set_transform_rotates_text")
3435
class ClabelText(Text):
3536
"""
3637
Unlike the ordinary text, the get_rotation returns an updated
@@ -150,10 +151,8 @@ def clabel(self, levels=None, *,
150151
or minus 90 degrees from level.
151152
152153
use_clabeltext : bool, default: False
153-
If ``True``, `.ClabelText` class (instead of `.Text`) is used to
154-
create labels. `ClabelText` recalculates rotation angles
155-
of texts during the drawing time, therefore this can be used if
156-
aspect of the axes changes.
154+
If ``True``, use `.Text.set_transform_rotates_text` to ensure that
155+
label rotation is updated whenever the axes aspect changes.
157156
158157
zorder : float or None, default: ``(2 + contour.get_zorder())``
159158
zorder of the contour labels.
@@ -272,6 +271,7 @@ def get_label_width(self, lev, fmt, fsize):
272271
width *= 72 / fig.dpi
273272
return width
274273

274+
@_api.deprecated("3.7", alternative="Artist.set")
275275
def set_label_props(self, label, text, color):
276276
"""Set the label properties - color, fontsize, text."""
277277
label.set_text(text)
@@ -416,56 +416,32 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
416416

417417
return rotation, nlc
418418

419-
def _get_label_text(self, x, y, rotation):
420-
dx, dy = self.axes.transData.inverted().transform((x, y))
421-
return Text(dx, dy, rotation=rotation,
422-
horizontalalignment='center',
423-
verticalalignment='center', zorder=self._clabel_zorder)
424-
425-
def _get_label_clabeltext(self, x, y, rotation):
426-
# x, y, rotation is given in pixel coordinate. Convert them to
427-
# the data coordinate and create a label using ClabelText
428-
# class. This way, the rotation of the clabel is along the
429-
# contour line always.
430-
transDataInv = self.axes.transData.inverted()
431-
dx, dy = transDataInv.transform((x, y))
432-
drotation = transDataInv.transform_angles(np.array([rotation]),
433-
np.array([[x, y]]))
434-
t = ClabelText(dx, dy, rotation=drotation[0],
435-
horizontalalignment='center',
436-
verticalalignment='center', zorder=self._clabel_zorder)
437-
438-
return t
439-
440-
def _add_label(self, t, x, y, lev, cvalue):
441-
color = self.labelMappable.to_rgba(cvalue, alpha=self.alpha)
442-
443-
_text = self.get_text(lev, self.labelFmt)
444-
self.set_label_props(t, _text, color)
419+
def add_label(self, x, y, rotation, lev, cvalue):
420+
"""Add contour label without `.Text.set_transform_rotates_text`."""
421+
data_x, data_y = self.axes.transData.inverted().transform((x, y))
422+
t = Text(
423+
data_x, data_y,
424+
text=self.get_text(lev, self.labelFmt),
425+
rotation=rotation,
426+
horizontalalignment='center', verticalalignment='center',
427+
zorder=self._clabel_zorder,
428+
color=self.labelMappable.to_rgba(cvalue, alpha=self.alpha),
429+
fontproperties=self.labelFontProps,
430+
clip_box=self.axes.bbox)
445431
self.labelTexts.append(t)
446432
self.labelCValues.append(cvalue)
447433
self.labelXYs.append((x, y))
448-
449434
# Add label to plot here - useful for manual mode label selection
450435
self.axes.add_artist(t)
451436

452-
def add_label(self, x, y, rotation, lev, cvalue):
453-
"""
454-
Add contour label using :class:`~matplotlib.text.Text` class.
455-
"""
456-
t = self._get_label_text(x, y, rotation)
457-
self._add_label(t, x, y, lev, cvalue)
458-
459437
def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
460-
"""
461-
Add contour label using :class:`ClabelText` class.
462-
"""
463-
# x, y, rotation is given in pixel coordinate. Convert them to
464-
# the data coordinate and create a label using ClabelText
465-
# class. This way, the rotation of the clabel is along the
466-
# contour line always.
467-
t = self._get_label_clabeltext(x, y, rotation)
468-
self._add_label(t, x, y, lev, cvalue)
438+
"""Add contour label with `.Text.set_transform_rotates_text`."""
439+
self.add_label(x, y, rotation, lev, cvalue)
440+
# Grab the last added text, and reconfigure its rotation.
441+
t = self.labelTexts[-1]
442+
data_rotation, = self.axes.transData.inverted().transform_angles(
443+
[rotation], [[x, y]])
444+
t.set(rotation=data_rotation, transform_rotates_text=True)
469445

470446
def add_label_near(self, x, y, inline=True, inline_spacing=5,
471447
transform=None):

0 commit comments

Comments
 (0)