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

Skip to content

Commit 7473396

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 6c9c44d commit 7473396

File tree

2 files changed

+29
-48
lines changed

2 files changed

+29
-48
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``contour.ClabelText`` and ``ContourLabeler.set_label_props``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... are deprecated.
4+
5+
Use `.Text.set_transform_rotates_text` as a replacement for the former and
6+
`.Artist.set` as a replacement for the latter.

lib/matplotlib/contour.py

Lines changed: 23 additions & 48 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.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.
@@ -273,6 +272,7 @@ def get_label_width(self, lev, fmt, fsize):
273272
width *= 72 / fig.dpi
274273
return width
275274

275+
@_api.deprecated("3.7", alternative="Artist.set")
276276
def set_label_props(self, label, text, color):
277277
"""Set the label properties - color, fontsize, text."""
278278
label.set_text(text)
@@ -417,57 +417,32 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
417417

418418
return rotation, nlc
419419

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

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

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

0 commit comments

Comments
 (0)