Skip to content

Add SubplotSpec.add_subplot. #13280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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])`
24 changes: 24 additions & 0 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down