Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84292c4

Browse files
jklymakefiring
authored andcommitted
FIX: make set_text(None) keep string empty instead of "None" (#10392)
* FIX: make set_text(None) keep string empty
1 parent e12ce9f commit 84292c4

4 files changed

Lines changed: 35 additions & 8 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
`Text.set_text` with string argument ``None`` sets string to empty
2+
------------------------------------------------------------------
3+
4+
`Text.set_text` when passed a string value of ``None`` would set the
5+
string to ``"None"``, so subsequent calls to `Text.get_text` would return
6+
the ambiguous ``"None"`` string.
7+
8+
This change sets text objects passed ``None`` to have empty strings, so that
9+
`Text.get_text` returns and an empty string.

lib/matplotlib/legend.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,16 +1103,18 @@ def set_title(self, title, prop=None):
11031103
with *prop* parameter.
11041104
"""
11051105
self._legend_title_box._text.set_text(title)
1106+
if title:
1107+
self._legend_title_box._text.set_visible(True)
1108+
self._legend_title_box.set_visible(True)
1109+
else:
1110+
self._legend_title_box._text.set_visible(False)
1111+
self._legend_title_box.set_visible(False)
11061112

11071113
if prop is not None:
11081114
if isinstance(prop, dict):
11091115
prop = FontProperties(**prop)
11101116
self._legend_title_box._text.set_fontproperties(prop)
11111117

1112-
if title:
1113-
self._legend_title_box.set_visible(True)
1114-
else:
1115-
self._legend_title_box.set_visible(False)
11161118
self.stale = True
11171119

11181120
def get_title(self):

lib/matplotlib/tests/test_legend.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,14 @@ def test_shadow_framealpha():
517517
ax.plot(range(100), label="test")
518518
leg = ax.legend(shadow=True, facecolor='w')
519519
assert leg.get_frame().get_alpha() == 1
520+
521+
522+
def test_legend_title_empty():
523+
# test that if we don't set the legend title, that
524+
# it comes back as an empty string, and that it is not
525+
# visible:
526+
fig, ax = plt.subplots()
527+
ax.plot(range(10))
528+
leg = ax.legend()
529+
assert leg.get_title().get_text() == ""
530+
assert leg.get_title().get_visible() is False

lib/matplotlib/text.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def __init__(self,
158158
elif isinstance(fontproperties, six.string_types):
159159
fontproperties = FontProperties(fontproperties)
160160

161+
self._text = ''
161162
self.set_text(text)
162163
self.set_color(color)
163164
self.set_usetex(usetex)
@@ -1158,14 +1159,18 @@ def set_verticalalignment(self, align):
11581159

11591160
def set_text(self, s):
11601161
"""
1161-
Set the text string *s*
1162+
Set the text string *s*.
11621163
11631164
It may contain newlines (``\\n``) or math in LaTeX syntax.
11641165
1165-
ACCEPTS: string or anything printable with '%s' conversion.
1166+
ACCEPTS: string or object castable to string, except
1167+
``None``, which is set to an empty string.
11661168
"""
1167-
self._text = '%s' % (s,)
1168-
self.stale = True
1169+
if s is None:
1170+
s = ''
1171+
if s != self._text:
1172+
self._text = '%s' % (s,)
1173+
self.stale = True
11691174

11701175
@staticmethod
11711176
def is_math_text(s, usetex=None):

0 commit comments

Comments
 (0)