|
31 | 31 | # per level. |
32 | 32 |
|
33 | 33 |
|
| 34 | +@_api.deprecated("3.7", alternative="Text.set_transform_rotates_text") |
34 | 35 | class ClabelText(text.Text): |
35 | 36 | """ |
36 | 37 | Unlike the ordinary text, the get_rotation returns an updated |
@@ -150,10 +151,8 @@ def clabel(self, levels=None, *, |
150 | 151 | or minus 90 degrees from level. |
151 | 152 |
|
152 | 153 | 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. |
157 | 156 |
|
158 | 157 | zorder : float or None, default: ``(2 + contour.get_zorder())`` |
159 | 158 | zorder of the contour labels. |
@@ -273,6 +272,7 @@ def get_label_width(self, lev, fmt, fsize): |
273 | 272 | width *= 72 / fig.dpi |
274 | 273 | return width |
275 | 274 |
|
| 275 | + @_api.deprecated("3.7", alternative="Artist.set") |
276 | 276 | def set_label_props(self, label, text, color): |
277 | 277 | """Set the label properties - color, fontsize, text.""" |
278 | 278 | label.set_text(text) |
@@ -417,57 +417,32 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5): |
417 | 417 |
|
418 | 418 | return rotation, nlc |
419 | 419 |
|
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) |
447 | 432 | self.labelTexts.append(t) |
448 | 433 | self.labelCValues.append(cvalue) |
449 | 434 | self.labelXYs.append((x, y)) |
450 | | - |
451 | 435 | # Add label to plot here - useful for manual mode label selection |
452 | 436 | self.axes.add_artist(t) |
453 | 437 |
|
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 | | - |
461 | 438 | 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) |
471 | 446 |
|
472 | 447 | def add_label_near(self, x, y, inline=True, inline_spacing=5, |
473 | 448 | transform=None): |
|
0 commit comments