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("")