Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 164d0bb

Browse files
committed
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).
1 parent 2660901 commit 164d0bb

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
GridSpec items can now add subplots to their parent Figure directly
2+
```````````````````````````````````````````````````````````````````
3+
4+
`SubplotSpec` gained an ``add_subplot`` method, which allows one to write ::
5+
6+
fig = plt.figure()
7+
gs = fig.add_gridspec(2, 2)
8+
gs[0, 0].add_subplot() # instead of `fig.add_subplot(gs[0, 0])`

lib/matplotlib/gridspec.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ def __init__(self, nrows, ncols,
371371
name=subspeclb.name + '.gridspec' + layoutbox.seq_id(),
372372
artist=self)
373373

374+
@property
375+
def figure(self):
376+
return self.get_topmost_subplotspec().gridspec.figure
377+
374378
def get_subplot_params(self, figure=None):
375379
"""Return a dictionary of subplot layout parameters.
376380
"""
@@ -542,3 +546,23 @@ def subgridspec(self, nrows, ncols, **kwargs):
542546
"""
543547

544548
return GridSpecFromSubplotSpec(nrows, ncols, self, **kwargs)
549+
550+
def add_subplot(self, **kwargs):
551+
"""
552+
Add the subplot specified by *self* to the parent figure.
553+
554+
Note that the parent `GridSpec` must have its ``.parent`` attribute set
555+
for this method to work; otherwise, a ValueError is raised.
556+
557+
Keyword arguments are forwarded to `Figure.add_subplot`.
558+
559+
Example
560+
-------
561+
562+
gs = plt.figure().add_gridspec(2, 2)
563+
ax = gs[0, 0].add_subplot()
564+
"""
565+
if self._gridspec.figure is None:
566+
raise ValueError("add_subplot() only works for GridSpecs created "
567+
"with a parent Figure")
568+
return self._gridspec.figure.add_subplot(self, **kwargs)

lib/matplotlib/tests/test_tightlayout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ def test_tight_layout6():
107107

108108
gs1.tight_layout(fig, rect=[0, 0, 0.5, 1])
109109

110-
gs2 = gridspec.GridSpec(3, 1)
110+
gs2 = fig.add_gridspec(3, 1)
111111

112112
for ss in gs2:
113-
ax = fig.add_subplot(ss)
113+
ax = ss.add_subplot()
114114
example_plot(ax)
115115
ax.set_title("")
116116
ax.set_xlabel("")

0 commit comments

Comments
 (0)