Skip to content

FIX: make set_text(None) keep string empty instead of "None" #10392

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
merged 4 commits into from
Feb 14, 2018
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
9 changes: 9 additions & 0 deletions doc/api/next_api_changes/2018-02-07-JMK.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
`Text.set_text` with string argument ``None`` sets string to empty
------------------------------------------------------------------

`Text.set_text` when passed a string value of ``None`` would set the
string to ``"None"``, so subsequent calls to `Text.get_text` would return
the ambiguous ``"None"`` string.

This change sets text objects passed ``None`` to have empty strings, so that
`Text.get_text` returns and an empty string.
10 changes: 6 additions & 4 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,16 +1103,18 @@ def set_title(self, title, prop=None):
with *prop* parameter.
"""
self._legend_title_box._text.set_text(title)
if title:
self._legend_title_box._text.set_visible(True)
self._legend_title_box.set_visible(True)
else:
self._legend_title_box._text.set_visible(False)
self._legend_title_box.set_visible(False)

if prop is not None:
if isinstance(prop, dict):
prop = FontProperties(**prop)
self._legend_title_box._text.set_fontproperties(prop)

if title:
self._legend_title_box.set_visible(True)
else:
self._legend_title_box.set_visible(False)
self.stale = True

def get_title(self):
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,14 @@ def test_shadow_framealpha():
ax.plot(range(100), label="test")
leg = ax.legend(shadow=True, facecolor='w')
assert leg.get_frame().get_alpha() == 1


def test_legend_title_empty():
# test that if we don't set the legend title, that
# it comes back as an empty string, and that it is not
# visible:
fig, ax = plt.subplots()
ax.plot(range(10))
leg = ax.legend()
assert leg.get_title().get_text() == ""
Copy link
Member

Choose a reason for hiding this comment

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

assert not leg.get_title().get_text()

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should rely on boolean-ness here.

Copy link
Member

Choose a reason for hiding this comment

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

This is PEP-8 convention

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

But I'm ok, if you want to have an explicit comparison.

assert leg.get_title().get_visible() is False
13 changes: 9 additions & 4 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def __init__(self,
elif isinstance(fontproperties, six.string_types):
fontproperties = FontProperties(fontproperties)

self._text = ''
self.set_text(text)
self.set_color(color)
self.set_usetex(usetex)
Expand Down Expand Up @@ -1158,14 +1159,18 @@ def set_verticalalignment(self, align):

def set_text(self, s):
"""
Set the text string *s*
Set the text string *s*.

It may contain newlines (``\\n``) or math in LaTeX syntax.

ACCEPTS: string or anything printable with '%s' conversion.
ACCEPTS: string or object castable to string, except
``None``, which is set to an empty string.
"""
self._text = '%s' % (s,)
self.stale = True
if s is None:
s = ''
if s != self._text:
self._text = '%s' % (s,)
self.stale = True

@staticmethod
def is_math_text(s, usetex=None):
Expand Down