From 56d256111c7a2322943abc69a6f4e8401893a0d5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 23 Jul 2015 11:20:08 -0400 Subject: [PATCH 1/2] ENH: add remove call back to axes ax.remove() now works as expected --- lib/matplotlib/figure.py | 2 ++ lib/matplotlib/tests/test_figure.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 073d6889f74a..ca33b4010db6 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -908,6 +908,7 @@ def add_axes(self, *args, **kwargs): self._axstack.add(key, a) self.sca(a) + a._remove_method = lambda ax: self.delaxes(ax) self.stale = True return a @@ -996,6 +997,7 @@ def add_subplot(self, *args, **kwargs): self._axstack.add(key, a) self.sca(a) + a._remove_method = lambda ax: self.delaxes(ax) self.stale = True return a diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index d8f9a654d1c9..37f4b1ba62c1 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -4,7 +4,7 @@ from matplotlib.externals import six from matplotlib.externals.six.moves import xrange -from nose.tools import assert_equal, assert_true, assert_raises +from nose.tools import assert_equal, assert_true from matplotlib.testing.decorators import image_comparison, cleanup from matplotlib.axes import Axes import matplotlib.pyplot as plt @@ -181,6 +181,16 @@ def test_set_fig_size(): assert_equal(fig.get_figheight(), 3) +@cleanup +def test_axes_remove(): + fig, axes = plt.subplots(2, 2) + axes[-1, -1].remove() + for ax in axes.ravel()[:-1]: + assert ax in fig.axes + assert axes[-1, -1] not in fig.axes + assert_equal(len(fig.axes), 3) + + if __name__ == "__main__": import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From 555e03e748d5fdd6f0e04e5659bafec9f216bc70 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 23 Jul 2015 12:45:35 -0400 Subject: [PATCH 2/2] DOC: add whats_new --- doc/users/whats_new/2015-07-23_axes_remove_method.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 doc/users/whats_new/2015-07-23_axes_remove_method.rst diff --git a/doc/users/whats_new/2015-07-23_axes_remove_method.rst b/doc/users/whats_new/2015-07-23_axes_remove_method.rst new file mode 100644 index 000000000000..759445288048 --- /dev/null +++ b/doc/users/whats_new/2015-07-23_axes_remove_method.rst @@ -0,0 +1,5 @@ +`ax.remove()` works as expected +------------------------------- + +As with artists added to as `Axes`, `Axes` can be +removed from their figure via ``ax.remove()``