From fbcaec535604ef79573b6d3c9a01c04877ab9a44 Mon Sep 17 00:00:00 2001 From: Cimarron Mittelsteadt Date: Sun, 9 Feb 2014 11:43:11 -0800 Subject: [PATCH 1/4] ENH: Added remove methods for legends in figure and axes objects --- lib/matplotlib/axes/_axes.py | 1 + lib/matplotlib/figure.py | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 47645f94c1fa..a4541503d9a0 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -509,6 +509,7 @@ def legend(self, *args, **kwargs): raise TypeError('Invalid arguments to legend.') self.legend_ = mlegend.Legend(self, handles, labels, **kwargs) + self.legend_._remove_method = lambda h: setattr(self, 'legend_', None) return self.legend_ def text(self, x, y, s, fontdict=None, diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 34f529ecb044..c6730642b3c5 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1166,6 +1166,7 @@ def legend(self, handles, labels, *args, **kwargs): """ l = Legend(self, handles, labels, *args, **kwargs) self.legends.append(l) + l._remove_method = lambda h: self.legends.remove(h) return l @docstring.dedent_interpd From e7f051f31e08e26c94341bdf5f34300b5fd76428 Mon Sep 17 00:00:00 2001 From: Cimarron Mittelsteadt Date: Sun, 9 Feb 2014 13:22:30 -0800 Subject: [PATCH 2/4] TST: Added tests for remove of legends on figure and axes objects --- lib/matplotlib/tests/test_legend.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 15dadeb6c75f..935be943b49a 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -5,7 +5,7 @@ from six.moves import xrange import mock -from nose.tools import assert_equal +from nose.tools import assert_equal, assert_is import numpy as np from matplotlib.testing.decorators import image_comparison, cleanup @@ -113,6 +113,19 @@ def test_legend_expand(): ax.legend(loc=3, mode=mode, ncol=2) +@cleanup +def test_legend_remove(): + fig = plt.figure() + ax = fig.add_subplot(1, 1, 1) + lines = ax.plot(range(10)) + leg = fig.legend(lines, "test") + leg.remove() + assert_equal(fig.legends, []) + leg = ax.legend("test") + leg.remove() + assert_is(ax.get_legend(), None) + + class TestLegendFunction(object): # Tests the legend function on the Axes and pyplot. From 4ee1b31d8ca0b7f2f6b0ab5a28dc69c2491d05dd Mon Sep 17 00:00:00 2001 From: Cimarron Mittelsteadt Date: Mon, 10 Feb 2014 09:15:31 -0800 Subject: [PATCH 3/4] BLD: Replaced assert_is import from nose.tools in test_legend as python 2.6 does not support it --- lib/matplotlib/tests/test_legend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 935be943b49a..dd6177e06078 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -5,7 +5,7 @@ from six.moves import xrange import mock -from nose.tools import assert_equal, assert_is +from nose.tools import assert_equal import numpy as np from matplotlib.testing.decorators import image_comparison, cleanup @@ -123,7 +123,7 @@ def test_legend_remove(): assert_equal(fig.legends, []) leg = ax.legend("test") leg.remove() - assert_is(ax.get_legend(), None) + assert ax.get_legend() is None class TestLegendFunction(object): From d1ad5d2681aefdca0b6e5a884c45d6e3f457b891 Mon Sep 17 00:00:00 2001 From: Cimarron Mittelsteadt Date: Mon, 10 Feb 2014 09:16:19 -0800 Subject: [PATCH 4/4] STY: pep8 fixes to test_legend.py --- lib/matplotlib/tests/test_legend.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index dd6177e06078..d4eeae2e34f1 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -80,21 +80,21 @@ def test_framealpha(): plt.legend(framealpha=0.5) -@image_comparison(baseline_images=['scatter_rc3','scatter_rc1'], remove_text=True) +@image_comparison(baseline_images=['scatter_rc3', 'scatter_rc1'], remove_text=True) def test_rc(): # using subplot triggers some offsetbox functionality untested elsewhere fig = plt.figure() - ax = plt.subplot(121) + ax = plt.subplot(121) ax.scatter(list(xrange(10)), list(xrange(10, 0, -1)), label='three') ax.legend(loc="center left", bbox_to_anchor=[1.0, 0.5], - title="My legend") + title="My legend") mpl.rcParams['legend.scatterpoints'] = 1 fig = plt.figure() - ax = plt.subplot(121) + ax = plt.subplot(121) ax.scatter(list(xrange(10)), list(xrange(10, 0, -1)), label='one') ax.legend(loc="center left", bbox_to_anchor=[1.0, 0.5], - title="My legend") + title="My legend") @image_comparison(baseline_images=['legend_expand'], remove_text=True) @@ -167,8 +167,8 @@ def __call__(self, legend, orig_handle, fontsize, handlebox): handler_map={None: AnyObjectHandler()}) warn.assert_called_with(u'Legend handers must now implement a ' - '"legend_artist" method rather than ' - 'being a callable.', + '"legend_artist" method rather than ' + 'being a callable.', MatplotlibDeprecationWarning, stacklevel=1)