Skip to content

Commit 2465eff

Browse files
authored
Merge pull request matplotlib#19228 from dstansby/validate-rotation
Validate text rotation in setter
2 parents 849c5db + 02ec590 commit 2465eff

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

examples/text_labels_and_annotations/watermark_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
ax.text(0.5, 0.5, 'created with matplotlib', transform=ax.transAxes,
2020
fontsize=40, color='gray', alpha=0.5,
21-
ha='center', va='center', rotation='30')
21+
ha='center', va='center', rotation=30)
2222

2323
plt.show()
2424

lib/matplotlib/tests/test_figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def test_add_subplot_invalid():
303303
def test_suptitle():
304304
fig, _ = plt.subplots()
305305
fig.suptitle('hello', color='r')
306-
fig.suptitle('title', color='g', rotation='30')
306+
fig.suptitle('title', color='g', rotation=30)
307307

308308

309309
def test_suptitle_fontproperties():

lib/matplotlib/tests/test_text.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,14 @@ def test_update_mutate_input():
711711
assert inp['bbox'] == cache['bbox']
712712

713713

714+
@pytest.mark.parametrize('rotation', ['invalid string', [90]])
715+
def test_invalid_rotation_values(rotation):
716+
with pytest.raises(
717+
ValueError,
718+
match=("rotation must be 'vertical', 'horizontal' or a number")):
719+
Text(0, 0, 'foo', rotation=rotation)
720+
721+
714722
def test_invalid_color():
715723
with pytest.raises(ValueError):
716724
plt.figtext(.5, .5, "foo", c="foobar")

lib/matplotlib/text.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
import math
7+
import numbers
78
import weakref
89

910
import numpy as np
@@ -149,7 +150,7 @@ def __init__(self,
149150
self.set_verticalalignment(verticalalignment)
150151
self.set_horizontalalignment(horizontalalignment)
151152
self._multialignment = multialignment
152-
self._rotation = rotation
153+
self.set_rotation(rotation)
153154
self._transform_rotates_text = transform_rotates_text
154155
self._bbox_patch = None # a FancyBboxPatch instance
155156
self._renderer = None
@@ -1177,6 +1178,11 @@ def set_rotation(self, s):
11771178
The rotation angle in degrees in mathematically positive direction
11781179
(counterclockwise). 'horizontal' equals 0, 'vertical' equals 90.
11791180
"""
1181+
if (s is not None and
1182+
not isinstance(s, numbers.Real) and
1183+
s not in ['vertical', 'horizontal']):
1184+
raise ValueError("rotation must be 'vertical', 'horizontal' or "
1185+
f"a number, not {s}")
11801186
self._rotation = s
11811187
self.stale = True
11821188

0 commit comments

Comments
 (0)