ENH: Add gridspec method to figure, and subplotspecs · matplotlib/matplotlib@2977f37 · GitHub
Skip to content

Commit 2977f37

Browse files
committed
ENH: Add gridspec method to figure, and subplotspecs
1 parent edd053d commit 2977f37

6 files changed

Lines changed: 227 additions & 76 deletions

File tree

doc/users/whats_new.rst

Lines changed: 19 additions & 1 deletion

lib/matplotlib/figure.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ def __init__(self,
394394
self._align_xlabel_grp = cbook.Grouper()
395395
self._align_ylabel_grp = cbook.Grouper()
396396

397+
# list of child gridspecs for this figure
398+
self._gridspecs = []
399+
397400
# TODO: I'd like to dynamically add the _repr_html_ method
398401
# to the figure in the right context, but then IPython doesn't
399402
# use it, for some reason.
@@ -1480,6 +1483,7 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
14801483
else:
14811484
# this should turn constrained_layout off if we don't want it
14821485
gs = GridSpec(nrows, ncols, figure=None, **gridspec_kw)
1486+
self._gridspecs.append(gs)
14831487

14841488
# Create array to hold all axes.
14851489
axarr = np.empty((nrows, ncols), dtype=object)
@@ -2474,6 +2478,49 @@ def align_labels(self, axs=None):
24742478
self.align_xlabels(axs=axs)
24752479
self.align_ylabels(axs=axs)
24762480

2481+
def add_gridspec(self, nrows, ncols, **kwargs):
2482+
"""
2483+
Return a `.GridSpec` that has this figure as a parent. This allows
2484+
complex layout of axes in the figure.
2485+
2486+
Parameters
2487+
----------
2488+
nrows : int
2489+
Number of rows in grid.
2490+
2491+
ncols : int
2492+
Number or columns in grid.
2493+
2494+
Returns
2495+
-------
2496+
gridspec : `.GridSpec`
2497+
2498+
Other Parameters
2499+
----------------
2500+
*kwargs* are passed to `.GridSpec`.
2501+
2502+
See Also
2503+
--------
2504+
matplotlib.pyplot.subplots
2505+
2506+
Examples
2507+
--------
2508+
Adding a subplot that spans two rows::
2509+
2510+
fig = plt.figure()
2511+
gs = fig.add_gridspec(2, 2)
2512+
ax1 = fig.add_subplot(gs[0, 0])
2513+
ax2 = fig.add_subplot(gs[1, 0])
2514+
# spans two rows:
2515+
ax3 = fig.add_subplot(gs[:, 1])
2516+
2517+
"""
2518+
2519+
_ = kwargs.pop('figure', None) # pop in case user has added this...
2520+
gs = GridSpec(nrows=nrows, ncols=ncols, figure=self, **kwargs)
2521+
self._gridspecs.append(gs)
2522+
return gs
2523+
24772524

24782525
def figaspect(arg):
24792526
"""

lib/matplotlib/gridspec.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,44 @@ def __eq__(self, other):
498498

499499
def __hash__(self):
500500
return hash((self._gridspec, self.num1, self.num2))
501+
502+
def subgridspec(self, nrows, ncols, **kwargs):
503+
"""
504+
Return a `.GridSpecFromSubplotSpec` that has this subplotspec as
505+
a parent.
506+
507+
Parameters
508+
----------
509+
nrows : int
510+
Number of rows in grid.
511+
512+
ncols : int
513+
Number or columns in grid.
514+
515+
Returns
516+
-------
517+
gridspec : `.GridSpec`
518+
519+
Other Parameters
520+
----------------
521+
**kwargs
522+
All other parameters are passed to `.GridSpec`.
523+
524+
See Also
525+
--------
526+
matplotlib.pyplot.subplots
527+
528+
Examples
529+
--------
530+
Adding three subplots in the space occupied by a single subplot::
531+
532+
fig = plt.figure()
533+
gs0 = fig.add_gridspec(3, 1)
534+
ax1 = fig.add_subplot(gs0[0])
535+
ax2 = fig.add_subplot(gs0[1])
536+
gssub = gs0[2].subgridspec(1, 3)
537+
for i in range(3):
538+
fig.add_subplot(gssub[0, i])
539+
"""
540+
541+
return GridSpecFromSubplotSpec(nrows, ncols, self, **kwargs)

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def test_constrained_layout5():
9494
def test_constrained_layout6():
9595
'Test constrained_layout for nested gridspecs'
9696
fig = plt.figure(constrained_layout=True)
97-
gs = gridspec.GridSpec(1, 2, figure=fig)
98-
gsl = gridspec.GridSpecFromSubplotSpec(2, 2, gs[0])
99-
gsr = gridspec.GridSpecFromSubplotSpec(1, 2, gs[1])
97+
gs = fig.add_gridspec(1, 2, figure=fig)
98+
gsl = gs[0].subgridspec(2, 2)
99+
gsr = gs[1].subgridspec(1, 2)
100100
axsl = []
101101
for gs in gsl:
102102
ax = fig.add_subplot(gs)

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 24 additions & 21 deletions

0 commit comments

Comments
 (0)