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

Sign up
Appearance settings

Validate text rotation in setter #19228

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

Merged
QuLogic merged 1 commit into matplotlib:main from dstansby:validate-rotation
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/text_labels_and_annotations/watermark_text.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

plt.show()

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_figure.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def test_add_subplot_invalid():
def test_suptitle():
fig, _ = plt.subplots()
fig.suptitle('hello', color='r')
fig.suptitle('title', color='g', rotation='30')
fig.suptitle('title', color='g', rotation=30)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess strictly speaking this is backcompat breaking? I doubt it's worth a deprecation period, but it should be noted.

Copy link
Member

@timhoffm timhoffm Mar 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not even sure we need to mention it. '30' is not a supported format according to docs. It just happens to work. If we drop that without deprecation period, a note does not really help anybody. Nobody goes through the note and says "hey rotation does not take string numbers anymore. That's my thing, I'm doing that all the time". I'll bet anything if anybody passes '30', they will only notice by the error message they will get.

jklymak and anntzer reacted with thumbs up emoji


def test_suptitle_fontproperties():
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_text.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,14 @@ def test_update_mutate_input():
assert inp['bbox'] == cache['bbox']


@pytest.mark.parametrize('rotation', ['invalid string', [90]])
def test_invalid_rotation_values(rotation):
with pytest.raises(
ValueError,
match=("rotation must be 'vertical', 'horizontal' or a number")):
Text(0, 0, 'foo', rotation=rotation)


def test_invalid_color():
with pytest.raises(ValueError):
plt.figtext(.5, .5, "foo", c="foobar")
Expand Down
8 changes: 7 additions & 1 deletion lib/matplotlib/text.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
import math
import numbers
import weakref

import numpy as np
Expand Down Expand Up @@ -149,7 +150,7 @@ def __init__(self,
self.set_verticalalignment(verticalalignment)
self.set_horizontalalignment(horizontalalignment)
self._multialignment = multialignment
self._rotation = rotation
self.set_rotation(rotation)
self._transform_rotates_text = transform_rotates_text
self._bbox_patch = None # a FancyBboxPatch instance
self._renderer = None
Expand Down Expand Up @@ -1177,6 +1178,11 @@ def set_rotation(self, s):
The rotation angle in degrees in mathematically positive direction
(counterclockwise). 'horizontal' equals 0, 'vertical' equals 90.
"""
if (s is not None and
not isinstance(s, numbers.Real) and
s not in ['vertical', 'horizontal']):
raise ValueError("rotation must be 'vertical', 'horizontal' or "
f"a number, not {s}")
self._rotation = s
self.stale = True

Expand Down

AltStyle によって変換されたページ (->オリジナル) /