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

Skip to content

Replace ClabelText by set_transform_rotates_text. #24140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/next_api_changes/deprecations/24140-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
``contour.ClabelText`` and ``ContourLabeler.set_label_props``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... are deprecated.

Use ``Text(..., transform_rotates_text=True)`` as a replacement for
``contour.ClabelText(...)`` and ``text.set(text=text, color=color,
fontproperties=labeler.labelFontProps, clip_box=labeler.axes.bbox)`` as a
replacement for the ``ContourLabeler.set_label_props(label, text, color)``.
70 changes: 23 additions & 47 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# per level.


@_api.deprecated("3.7", alternative="Text.set_transform_rotates_text")
class ClabelText(Text):
"""
Unlike the ordinary text, the get_rotation returns an updated
Expand Down Expand Up @@ -150,10 +151,8 @@ def clabel(self, levels=None, *,
or minus 90 degrees from level.

use_clabeltext : bool, default: False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should one consider renaming this argument?

If ``True``, `.ClabelText` class (instead of `.Text`) is used to
create labels. `ClabelText` recalculates rotation angles
of texts during the drawing time, therefore this can be used if
aspect of the axes changes.
If ``True``, use `.Text.set_transform_rotates_text` to ensure that
label rotation is updated whenever the axes aspect changes.

zorder : float or None, default: ``(2 + contour.get_zorder())``
zorder of the contour labels.
Expand Down Expand Up @@ -272,6 +271,7 @@ def get_label_width(self, lev, fmt, fsize):
width *= 72 / fig.dpi
return width

@_api.deprecated("3.7", alternative="Artist.set")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deprecation is a bit terse

I think

labeler.set_label_props(label, text, color)

is equivalent to

label.set(text=text, color=color, fontproperties=labeler.labelFontProps, clip_box(labeler.axes)

Which is not quite straight forward unless you check the source. Can we document this better (even only in the changelog would be ok).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, done.

def set_label_props(self, label, text, color):
"""Set the label properties - color, fontsize, text."""
label.set_text(text)
Expand Down Expand Up @@ -416,56 +416,32 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):

return rotation, nlc

def _get_label_text(self, x, y, rotation):
dx, dy = self.axes.transData.inverted().transform((x, y))
return Text(dx, dy, rotation=rotation,
horizontalalignment='center',
verticalalignment='center', zorder=self._clabel_zorder)

def _get_label_clabeltext(self, x, y, rotation):
# x, y, rotation is given in pixel coordinate. Convert them to
# the data coordinate and create a label using ClabelText
# class. This way, the rotation of the clabel is along the
# contour line always.
transDataInv = self.axes.transData.inverted()
dx, dy = transDataInv.transform((x, y))
drotation = transDataInv.transform_angles(np.array([rotation]),
np.array([[x, y]]))
t = ClabelText(dx, dy, rotation=drotation[0],
horizontalalignment='center',
verticalalignment='center', zorder=self._clabel_zorder)

return t

def _add_label(self, t, x, y, lev, cvalue):
color = self.labelMappable.to_rgba(cvalue, alpha=self.alpha)

_text = self.get_text(lev, self.labelFmt)
self.set_label_props(t, _text, color)
def add_label(self, x, y, rotation, lev, cvalue):
"""Add contour label without `.Text.set_transform_rotates_text`."""
data_x, data_y = self.axes.transData.inverted().transform((x, y))
t = Text(
data_x, data_y,
text=self.get_text(lev, self.labelFmt),
rotation=rotation,
horizontalalignment='center', verticalalignment='center',
zorder=self._clabel_zorder,
color=self.labelMappable.to_rgba(cvalue, alpha=self.alpha),
fontproperties=self.labelFontProps,
clip_box=self.axes.bbox)
self.labelTexts.append(t)
self.labelCValues.append(cvalue)
self.labelXYs.append((x, y))

# Add label to plot here - useful for manual mode label selection
self.axes.add_artist(t)

def add_label(self, x, y, rotation, lev, cvalue):
"""
Add contour label using :class:`~matplotlib.text.Text` class.
"""
t = self._get_label_text(x, y, rotation)
self._add_label(t, x, y, lev, cvalue)

def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
"""
Add contour label using :class:`ClabelText` class.
"""
# x, y, rotation is given in pixel coordinate. Convert them to
# the data coordinate and create a label using ClabelText
# class. This way, the rotation of the clabel is along the
# contour line always.
t = self._get_label_clabeltext(x, y, rotation)
self._add_label(t, x, y, lev, cvalue)
"""Add contour label with `.Text.set_transform_rotates_text`."""
self.add_label(x, y, rotation, lev, cvalue)
# Grab the last added text, and reconfigure its rotation.
t = self.labelTexts[-1]
data_rotation, = self.axes.transData.inverted().transform_angles(
[rotation], [[x, y]])
t.set(rotation=data_rotation, transform_rotates_text=True)

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