-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)``. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -150,10 +151,8 @@ def clabel(self, levels=None, *, | |
or minus 90 degrees from level. | ||
|
||
use_clabeltext : bool, default: False | ||
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. | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The deprecation is a bit terse I think
is equivalent to
Which is not quite straight forward unless you check the source. Can we document this better (even only in the changelog would be ok). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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): | ||
|
There was a problem hiding this comment.
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?