From 07d955e47b1c80c13712e3a45e154cdffc43d131 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 11 Oct 2022 11:26:44 +0200 Subject: [PATCH] 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). --- .../deprecations/24140-AL.rst | 8 +++ lib/matplotlib/contour.py | 70 ++++++------------- 2 files changed, 31 insertions(+), 47 deletions(-) create mode 100644 doc/api/next_api_changes/deprecations/24140-AL.rst diff --git a/doc/api/next_api_changes/deprecations/24140-AL.rst b/doc/api/next_api_changes/deprecations/24140-AL.rst new file mode 100644 index 000000000000..afe10ddc6475 --- /dev/null +++ b/doc/api/next_api_changes/deprecations/24140-AL.rst @@ -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)``. diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index f1a502bcf824..d6a46780c0c7 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -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") 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):