Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1f8f50e

Browse filesBrowse files
committed
Deprecate toplevel mpl.text.get_rotation; normalize rotations early.
get_rotation had been made a toplevel function a long time ago to be used in TextWithDash, which has now been removed, so there isn't much justification to have it separate. Also, note that while the toplevel get_rotation's implementation also supported string-form of floats (see test_text.test_get_rotation_string), this was not consistent with the docstring, and, more importantly, it was not possible to set a Text's rotation to e.g. "15." because Text.set_rotation() would first reject that anyways. Also, we can directly normalize angles in Text.set_rotation, rather than doing it again and again in Text.get_rotation. Again, this made the old inconsistency (on supporting string-form floats) clearer.
1 parent a093342 commit 1f8f50e
Copy full SHA for 1f8f50e

File tree

Expand file treeCollapse file tree

3 files changed

+22
-19
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+22
-19
lines changed
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``matplotlib.text.get_rotation()``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated with no replacement. Copy the original implementation if
4+
needed.

‎lib/matplotlib/tests/test_text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_text.py
+7-8Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -345,33 +345,32 @@ def test_non_default_dpi(text):
345345

346346

347347
def test_get_rotation_string():
348-
assert mpl.text.get_rotation('horizontal') == 0.
349-
assert mpl.text.get_rotation('vertical') == 90.
350-
assert mpl.text.get_rotation('15.') == 15.
348+
assert Text(rotation='horizontal').get_rotation() == 0.
349+
assert Text(rotation='vertical').get_rotation() == 90.
351350

352351

353352
def test_get_rotation_float():
354353
for i in [15., 16.70, 77.4]:
355-
assert mpl.text.get_rotation(i) == i
354+
assert Text(rotation=i).get_rotation() == i
356355

357356

358357
def test_get_rotation_int():
359358
for i in [67, 16, 41]:
360-
assert mpl.text.get_rotation(i) == float(i)
359+
assert Text(rotation=i).get_rotation() == float(i)
361360

362361

363362
def test_get_rotation_raises():
364363
with pytest.raises(ValueError):
365-
mpl.text.get_rotation('hozirontal')
364+
Text(rotation='hozirontal')
366365

367366

368367
def test_get_rotation_none():
369-
assert mpl.text.get_rotation(None) == 0.0
368+
assert Text(rotation=None).get_rotation() == 0.0
370369

371370

372371
def test_get_rotation_mod360():
373372
for i, j in zip([360., 377., 720+177.2], [0., 17., 177.2]):
374-
assert_almost_equal(mpl.text.get_rotation(i), j)
373+
assert_almost_equal(Text(rotation=i).get_rotation(), j)
375374

376375

377376
@pytest.mark.parametrize("ha", ["center", "right", "left"])

‎lib/matplotlib/text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/text.py
+11-11Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
_log = logging.getLogger(__name__)
2424

2525

26-
# Extracted from Text's method to serve as a function
26+
@_api.deprecated("3.6")
2727
def get_rotation(rotation):
2828
"""
2929
Return *rotation* normalized to an angle between 0 and 360 degrees.
@@ -240,13 +240,10 @@ def _get_multialignment(self):
240240
def get_rotation(self):
241241
"""Return the text angle in degrees between 0 and 360."""
242242
if self.get_transform_rotates_text():
243-
angle = get_rotation(self._rotation)
244-
x, y = self.get_unitless_position()
245-
angles = [angle, ]
246-
pts = [[x, y]]
247-
return self.get_transform().transform_angles(angles, pts).item(0)
243+
return self.get_transform().transform_angles(
244+
[self._rotation], [self.get_unitless_position()]).item(0)
248245
else:
249-
return get_rotation(self._rotation) # string_or_number -> number
246+
return self._rotation
250247

251248
def get_transform_rotates_text(self):
252249
"""
@@ -1176,12 +1173,15 @@ def set_rotation(self, s):
11761173
The rotation angle in degrees in mathematically positive direction
11771174
(counterclockwise). 'horizontal' equals 0, 'vertical' equals 90.
11781175
"""
1179-
if (s is not None and
1180-
not isinstance(s, numbers.Real) and
1181-
s not in ['vertical', 'horizontal']):
1176+
if isinstance(s, numbers.Real):
1177+
self._rotation = float(s) % 360
1178+
elif cbook._str_equal(s, 'horizontal') or s is None:
1179+
self._rotation = 0.
1180+
elif cbook._str_equal(s, 'vertical'):
1181+
self._rotation = 90.
1182+
else:
11821183
raise ValueError("rotation must be 'vertical', 'horizontal' or "
11831184
f"a number, not {s}")
1184-
self._rotation = s
11851185
self.stale = True
11861186

11871187
def set_transform_rotates_text(self, t):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.