diff --git a/doc/api/next_api_changes/2018-02-07-JMK.rst b/doc/api/next_api_changes/2018-02-07-JMK.rst new file mode 100644 index 000000000000..3a98bb67e5ad --- /dev/null +++ b/doc/api/next_api_changes/2018-02-07-JMK.rst @@ -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. diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 08b5c45de1cf..0f42a34c01fb 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -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): diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 55b8adc77745..b25cea273487 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -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() == "" + assert leg.get_title().get_visible() is False diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 7477916828fa..c15a0c08be80 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -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) @@ -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):