1
- """
2
- :mod:`~matplotlib.gridspec` is a module which specifies the location
3
- of the subplot in the figure.
4
-
5
- `GridSpec`
6
- specifies the geometry of the grid that a subplot will be
7
- placed. The number of rows and number of columns of the grid
8
- need to be set. Optionally, the subplot layout parameters
9
- (e.g., left, right, etc.) can be tuned.
1
+ r"""
2
+ :mod:`~matplotlib.gridspec` contains classes that help to layout multiple
3
+ `~axes.Axes` in a grid-like pattern within a figure.
10
4
11
- `SubplotSpec`
12
- specifies the location of the subplot in the given `GridSpec` .
5
+ The `GridSpec` specifies the overall grid structure. Individual cells within
6
+ the grid are referenced by `SubplotSpec`\s .
13
7
8
+ See the tutorial :ref:`sphx_glr_tutorials_intermediate_gridspec.py` for a
9
+ comprehensive usage guide.
14
10
"""
15
11
16
12
import copy
@@ -55,16 +51,16 @@ def __repr__(self):
55
51
)
56
52
57
53
def get_geometry (self ):
58
- 'get the geometry of the grid, e.g., 2,3'
54
+ """
55
+ Return a tuple containing the number of rows and columns in the grid.
56
+ """
59
57
return self ._nrows , self ._ncols
60
58
61
59
def get_subplot_params (self , figure = None , fig = None ):
62
60
pass
63
61
64
62
def new_subplotspec (self , loc , rowspan = 1 , colspan = 1 ):
65
- """
66
- Create and return a SubplotSpec instance.
67
- """
63
+ """Create and return a `.SubplotSpec` instance."""
68
64
loc1 , loc2 = loc
69
65
subplotspec = self [loc1 :loc1 + rowspan , loc2 :loc2 + colspan ]
70
66
return subplotspec
@@ -89,7 +85,7 @@ def get_height_ratios(self):
89
85
90
86
def get_grid_positions (self , fig , raw = False ):
91
87
"""
92
- return lists of bottom and top position of rows, left and
88
+ Return lists of bottom and top position of rows, left and
93
89
right positions of columns.
94
90
95
91
If raw=True, then these are all in units relative to the container
@@ -142,8 +138,7 @@ def get_grid_positions(self, fig, raw=False):
142
138
return fig_bottoms , fig_tops , fig_lefts , fig_rights
143
139
144
140
def __getitem__ (self , key ):
145
- """Create and return a SubplotSpec instance.
146
- """
141
+ """Create and return a `.SubplotSpec` instance."""
147
142
nrows , ncols = self .get_geometry ()
148
143
149
144
def _normalize (key , size , axis ): # Includes last index.
@@ -182,11 +177,10 @@ def _normalize(key, size, axis): # Includes last index.
182
177
183
178
class GridSpec (GridSpecBase ):
184
179
"""
185
- A class that specifies the geometry of the grid that a subplot
186
- will be placed. The location of grid is determined by similar way
187
- as the SubplotParams.
188
- """
180
+ Specifies the geometry of the grid that a subplot can be placed in.
189
181
182
+ The location of grid is determined by similar way as the SubplotParams.
183
+ """
190
184
def __init__ (self , nrows , ncols , figure = None ,
191
185
left = None , bottom = None , right = None , top = None ,
192
186
wspace = None , hspace = None ,
@@ -328,7 +322,7 @@ def tight_layout(self, figure, renderer=None,
328
322
fraction of the font-size.
329
323
h_pad, w_pad : float, optional
330
324
Padding (height/width) between edges of adjacent subplots.
331
- Defaults to ``pad_inches`` .
325
+ Defaults to *pad* .
332
326
rect : tuple of 4 floats, optional
333
327
(left, bottom, right, top) rectangle in normalized figure
334
328
coordinates that the whole subplots area (including labels) will
@@ -403,22 +397,33 @@ def get_subplot_params(self, figure=None):
403
397
wspace = wspace , hspace = hspace )
404
398
405
399
def get_topmost_subplotspec (self ):
406
- """Get the topmost SubplotSpec instance associated with the subplot."""
400
+ """
401
+ Return the topmost `.SubplotSpec` instance associated with the subplot.
402
+ """
407
403
return self ._subplot_spec .get_topmost_subplotspec ()
408
404
409
405
410
406
class SubplotSpec :
411
- """Specifies the location of the subplot in the given `GridSpec`.
412
407
"""
408
+ Specifies the location of a subplot in a `GridSpec`.
413
409
414
- def __init__ (self , gridspec , num1 , num2 = None ):
415
- """
410
+ .. note::
411
+
412
+ Likely, you'll never instantiate a `SubplotSpec` yourself. Instead you
413
+ will typically obtain one from a `GridSpec` using item-access.
414
+
415
+ Parameters
416
+ ----------
417
+ gridspec : `~matplotlib.gridspec.GridSpec`
418
+ The GridSpec, which the subplot is referencing.
419
+ num1, num2 : int
F438
tr>416
420
The subplot will occupy the num1-th cell of the given
417
421
gridspec. If num2 is provided, the subplot will span between
418
422
num1-th cell and num2-th cell *inclusive*.
419
423
420
424
The index starts from 0.
421
- """
425
+ """
426
+ def __init__ (self , gridspec , num1 , num2 = None ):
422
427
self ._gridspec = gridspec
423
428
self .num1 = num1
424
429
self .num2 = num2
@@ -464,18 +469,19 @@ def get_gridspec(self):
464
469
465
470
def get_geometry (self ):
466
471
"""
467
- Get the subplot geometry (`` n_rows, n_cols, start, stop``) .
472
+ Return the subplot geometry as tuple ``( n_rows, n_cols, start, stop)`` .
468
473
469
- start and stop are the index of the start and stop of the
470
- subplot.
474
+ The indices *start* and *stop* define the range of the subplot within
475
+ the `GridSpec`. *stop* is inclusive (i.e. for a single cell
476
+ ``start == stop``).
471
477
"""
472
478
rows , cols = self .get_gridspec ().get_geometry ()
473
479
return rows , cols , self .num1 , self .num2
474
480
475
481
def get_rows_columns (self ):
476
482
"""
477
- Get the subplot row and column numbers:
478
- (`` n_rows, n_cols, row_start, row_stop, col_start, col_stop``)
483
+ Return the subplot row and column numbers as a tuple
484
+ ``( n_rows, n_cols, row_start, row_stop, col_start, col_stop)``.
479
485
"""
480
486
gridspec = self .get_gridspec ()
481
487
nrows , ncols = gridspec .get_geometry ()
@@ -484,7 +490,8 @@ def get_rows_columns(self):
484
490
return nrows , ncols , row_start , row_stop , col_start , col_stop
485
491
486
492
def get_position (self , figure , return_all = False ):
487
- """Update the subplot position from ``figure.subplotpars``.
493
+ """
494
+ Update the subplot position from ``figure.subplotpars``.
488
495
"""
489
496
gridspec = self .get_gridspec ()
490
497
nrows , ncols = gridspec .get_geometry ()
@@ -504,14 +511,20 @@ def get_position(self, figure, return_all=False):
504
511
return figbox
505
512
506
513
def get_topmost_subplotspec (self ):
507
- 'get the topmost SubplotSpec instance associated with the subplot'
514
+ """
515
+ Return the topmost `SubplotSpec` instance associated with the subplot.
516
+ """
508
517
gridspec = self .get_gridspec ()
509
518
if hasattr (gridspec , "get_topmost_subplotspec" ):
510
519
return gridspec .get_topmost_subplotspec ()
511
520
else :
512
521
return self
513
522
514
523
def __eq__ (self , other ):
524
+ """
525
+ Two SubplotSpecs are considered equal if they refer to the same
526
+ position(s) in the same `GridSpec`.
527
+ """
515
528
# other may not even have the attributes we are checking.
516
529
return ((self ._gridspec , self .num1 , self .num2 )
517
530
== (getattr (other , "_gridspec" , object ()),
@@ -523,7 +536,9 @@ def __hash__(self):
523
536
524
537
def subgridspec (self , nrows , ncols , ** kwargs ):
525
538
"""
526
- Return a `.GridSpecFromSubplotSpec` that has this subplotspec as
539
+ Create a GridSpec within this subplot.
540
+
541
+ The created `.GridSpecFromSubplotSpec` will have this `SubplotSpec` as
527
542
a parent.
528
543
529
544
Parameters
@@ -536,12 +551,12 @@ def subgridspec(self, nrows, ncols, **kwargs):
536
551
537
552
Returns
538
553
-------
539
- gridspec : `.GridSpec `
554
+ gridspec : `.GridSpecFromSubplotSpec `
540
555
541
556
Other Parameters
542
557
----------------
543
558
**kwargs
544
- All other parameters are passed to `.GridSpec `.
559
+ All other parameters are passed to `.GridSpecFromSubplotSpec `.
545
560
546
561
See Also
547
562
--------
@@ -559,5 +574,4 @@ def subgridspec(self, nrows, ncols, **kwargs):
559
574
for i in range(3):
560
575
fig.add_subplot(gssub[0, i])
561
576
"""
562
-
563
577
return GridSpecFromSubplotSpec (nrows , ncols , self , ** kwargs )
0 commit comments