From 15e78ae485cce66d7416426694fa774799ca4668 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 24 Jan 2019 20:48:08 +0100 Subject: [PATCH] Add SubplotSpec.add_subplot. Now that the preferred(?) method of creating GridSpecs attaches the GridSpec to a given figure (figure.add_gridspec), calling `fig.add_subplot(gs[...])` effectively specifies the figure information twice. Hence, allow one to do ``` gs = fig.add_gridspec(2, 2) gs[0, 0].add_subplot() ``` as a synonym for ``` gs = fig.add_gridspec(2, 2) fig.add_subplot(gs[0, 0]) ``` Ideally, this should ultimately allow one to deprecate the somewhat unwieldy subplot2grid, replacing ``` plt.subplot2grid((3, 3), (0, 0)) plt.subplot2grid((3, 3), (1, 1), rowspan=2, colspan=2) ``` by ``` plt.figure().add_grispec(3, 3)[0, 0].add_subplot() plt.figure().add_grispec(3, 3)[1:, 1:].add_subplot() ``` or, after implementing a plt.add_gridspec() that operates on gcf(), ``` gs = plt.add_gridspec(3, 3) gs[0, 0].add_subplot() gs[1:, 1:].add_subplot() ``` A similar API change would be to make GridSpec.tight_layout() lose its `figure` argument (or rather, it could become optional). --- .../2018-01-24-AL-subplotspec-add_subplot.rst | 8 +++++++ lib/matplotlib/gridspec.py | 24 +++++++++++++++++++ lib/matplotlib/pyplot.py | 6 ++--- lib/matplotlib/tests/test_tightlayout.py | 4 ++-- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 doc/users/next_whats_new/2018-01-24-AL-subplotspec-add_subplot.rst diff --git a/doc/users/next_whats_new/2018-01-24-AL-subplotspec-add_subplot.rst b/doc/users/next_whats_new/2018-01-24-AL-subplotspec-add_subplot.rst new file mode 100644 index 000000000000..9a29047453d7 --- /dev/null +++ b/doc/users/next_whats_new/2018-01-24-AL-subplotspec-add_subplot.rst @@ -0,0 +1,8 @@ +GridSpec items can now add subplots to their parent Figure directly +``````````````````````````````````````````````````````````````````` + +`SubplotSpec` gained an ``add_subplot`` method, which allows one to write :: + + fig = plt.figure() + gs = fig.add_gridspec(2, 2) + gs[0, 0].add_subplot() # instead of `fig.add_subplot(gs[0, 0])` diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 883d9166c178..929642c6e5e7 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -371,6 +371,10 @@ def __init__(self, nrows, ncols, name=subspeclb.name + '.gridspec' + layoutbox.seq_id(), artist=self) + @property + def figure(self): + return self.get_topmost_subplotspec().gridspec.figure + def get_subplot_params(self, figure=None): """Return a dictionary of subplot layout parameters. """ @@ -542,3 +546,23 @@ def subgridspec(self, nrows, ncols, **kwargs): """ return GridSpecFromSubplotSpec(nrows, ncols, self, **kwargs) + + def add_subplot(self, **kwargs): + """ + Add the subplot specified by *self* to the parent figure. + + Note that the parent `GridSpec` must have its ``.parent`` attribute set + for this method to work; otherwise, a ValueError is raised. + + Keyword arguments are forwarded to `Figure.add_subplot`. + + Example + ------- + + gs = plt.figure().add_gridspec(2, 2) + ax = gs[0, 0].add_subplot() + """ + if self._gridspec.figure is None: + raise ValueError("add_subplot() only works for GridSpecs created " + "with a parent Figure") + return self._gridspec.figure.add_subplot(self, **kwargs) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index c61cf1f3e775..bdb2b08ad664 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -992,9 +992,9 @@ def subplot(*args, **kwargs): two subplots that are otherwise identical to be added to the figure, make sure you give them unique labels. - In rare circumstances, `.add_subplot` may be called with a single - argument, a subplot axes instance already created in the - present figure but not in the figure's list of axes. + In rare circumstances, `.subplot` may be called with a single argument, a + subplot axes instance already created in the present figure but not in the + figure's list of axes. See Also -------- diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index 5898c1a94aeb..2f0707269609 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -107,10 +107,10 @@ def test_tight_layout6(): gs1.tight_layout(fig, rect=[0, 0, 0.5, 1]) - gs2 = gridspec.GridSpec(3, 1) + gs2 = fig.add_gridspec(3, 1) for ss in gs2: - ax = fig.add_subplot(ss) + ax = ss.add_subplot() example_plot(ax) ax.set_title("") ax.set_xlabel("")