From bb9a33081f34c79c52f6ae9799958f19c75109ef Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 7 Feb 2018 09:09:19 -0800 Subject: [PATCH 1/4] FIX: make set_text(None) keep string empty --- doc/api/next_api_changes/2018-02-07-JMK.rst | 9 +++++++++ lib/matplotlib/legend.py | 10 ++++++---- lib/matplotlib/tests/test_legend.py | 11 +++++++++++ lib/matplotlib/text.py | 13 +++++++++---- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 doc/api/next_api_changes/2018-02-07-JMK.rst 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..86a76fc73a3c 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 anything printable with '%s' conversion, except + ``None``, which leaves the text string empty: """ - 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): From 7bbf4e577e895b5406559c4f7e610d7d3fadfb3e Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 7 Feb 2018 20:47:25 -0800 Subject: [PATCH 2/4] DOC: samll fix in dicstring --- lib/matplotlib/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 86a76fc73a3c..c15186d57cb3 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1164,7 +1164,7 @@ def set_text(self, s): It may contain newlines (``\\n``) or math in LaTeX syntax. ACCEPTS: string or anything printable with '%s' conversion, except - ``None``, which leaves the text string empty: + ``None``, which is set to an empty string. """ if s is None: s = '' From a2d5ffb2647a96ca02db4900ed3346cb12ef3dcb Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 8 Feb 2018 08:58:07 -0800 Subject: [PATCH 3/4] FIX: back to str(s), and doc fix --- lib/matplotlib/text.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index c15186d57cb3..b7b84f78fe96 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1163,13 +1163,13 @@ def set_text(self, s): It may contain newlines (``\\n``) or math in LaTeX syntax. - ACCEPTS: string or anything printable with '%s' conversion, except + ACCEPTS: string or object castable to string, except ``None``, which is set to an empty string. """ if s is None: s = '' if s != self._text: - self._text = '%s' % (s,) + self._text = str(s) self.stale = True @staticmethod From fd42972c7a8afc71a390c1c2b69620ef7765baf5 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 8 Feb 2018 10:58:31 -0800 Subject: [PATCH 4/4] FIX: back from str(s) --- lib/matplotlib/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index b7b84f78fe96..c15a0c08be80 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1169,7 +1169,7 @@ def set_text(self, s): if s is None: s = '' if s != self._text: - self._text = str(s) + self._text = '%s' % (s,) self.stale = True @staticmethod