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 th
E864
at 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 ,
@@ -329,7 +323,7 @@ def tight_layout(self, figure, renderer=None,
329
323
fraction of the font-size.
330
324
h_pad, w_pad : float, optional
331
325
Padding (height/width) between edges of adjacent subplots.
332
- Defaults to ``pad_inches`` .
326
+ Defaults to *pad* .
333
327
rect : tuple of 4 floats, optional
334
328
(left, bottom, right, top) rectangle in normalized figure
335
329
coordinates that the whole subplots area (including labels) will
@@ -404,22 +398,33 @@ def get_subplot_params(self, figure=None):
404
398
wspace = wspace , hspace = hspace )
405
399
406
400
def get_topmost_subplotspec (self ):
407
- """Get the topmost SubplotSpec instance associated with the subplot."""
401
+ """
402
+ Return the topmost `.SubplotSpec` instance associated with the subplot.
403
+ """
408
404
return self ._subplot_spec .get_topmost_subplotspec ()
409
405
410
406
411
- class SubplotSpec (object ):
412
- """Specifies the location of the subplot in the given `GridSpec`.
407
+ class SubplotSpec :
413
408
"""
409
+ Specifies the location of a subplot in a `GridSpec`.
414
410
415
- def __init__ (self , gridspec , num1 , num2 = None ):
416
- """
411
+ .. note::
412
+
413
+ Likely, you'll never instantiate a `SubplotSpec` yourself. Instead you
414
+ will typically obtain one from a `GridSpec` using item-access.
415
+
416
+ Parameters
417
+ ----------
418
+ gridspec : `~matplotlib.gridspec.GridSpec`
419
+ The GridSpec, which the subplot is referencing.
420
+ num1, num2 : int
417
421
The subplot will occupy the num1-th cell of the given
418
422
gridspec. If num2 is provided, the subplot will span between
419
423
num1-th cell and num2-th cell.
420
424
421
425
The index starts from 0.
422
- """
426
+ """
427
+ def __init__ (self , gridspec , num1 , num2 = None ):
423
428
self ._gridspec = gridspec
424
429
self .num1 = num1
425
430
self .num2 = num2
@@ -454,18 +459,19 @@ def get_gridspec(self):
454
459
455
460
def get_geometry (self ):
456
461
"""
457
- Get the subplot geometry (`` n_rows, n_cols, start, stop``) .
462
+ Return the subplot geometry as tuple ``( n_rows, n_cols, start, stop)`` .
458
463
459
- start and stop are the index of the start and stop of the
460
- subplot.
464
+ The indices *start* and *stop* define the range of the subplot within
465
+ the `GridSpec`. *stop* is inclusive (i.e. for a single cell
466
+ ``start == stop``).
461
467
"""
462
468
rows , cols = self .get_gridspec ().get_geometry ()
463
469
return rows , cols , self .num1 , self .num2
464
470
465
471
def get_rows_columns (self ):
466
472
"""
467
- Get the subplot row and column numbers:
468
- (`` n_rows, n_cols, row_start, row_stop, col_start, col_stop``)
473
+ Return the subplot row and column numbers as a tuple
474
+ ``( n_rows, n_cols, row_start, row_stop, col_start, col_stop)``.
469
475
"""
470
476
gridspec = self .get_gridspec ()
471
477
nrows , ncols = gridspec .get_geometry ()
@@ -478,7 +484,8 @@ def get_rows_columns(self):
478
484
return nrows , ncols , row_start , row_stop , col_start , col_stop
479
485
480
486
def get_position (self , figure , return_all = False ):
481
- """Update the subplot position from ``figure.subplotpars``.
487
+ """
488
+ Update the subplot position from ``figure.subplotpars``.
482
489
"""
483
490
gridspec = self .get_gridspec ()
484
491
nrows , ncols = gridspec .get_geometry ()
@@ -500,14 +507,20 @@ def get_position(self, figure, return_all=False):
500
507
return figbox
501
508
502
509
def get_topmost_subplotspec (self ):
503
- 'get the topmost SubplotSpec instance associated with the subplot'
510
+ """
511
+ Return the topmost `SubplotSpec` instance associated with the subplot.
512
+ """
504
513
gridspec = self .get_gridspec ()
505
514
if hasattr (gridspec , "get_topmost_subplotspec" ):
506
515
return gridspec .get_topmost_subplotspec ()
507
516
else :
508
517
return self
509
518
510
519
def __eq__ (self , other ):
520
+ """
521
+ Two SubplotSpecs are considered equal if they refer to the same
522
+ position(s) in the same `GridSpec`.
523
+ """
511
524
# other may not even have the attributes we are checking.
512
525
return ((self ._gridspec , self .num1 , self .num2 )
513
526
== (getattr (other , "_gridspec" , object ()),
@@ -519,7 +532,9 @@ def __hash__(self):
519
532
520
533
def subgridspec (self , nrows , ncols , ** kwargs ):
521
534
"""
522
- Return a `.GridSpecFromSubplotSpec` that has this subplotspec as
535
+ Create a GridSpec within this subplot.
536
+
537
+ The created `.GridSpecFromSubplotSpec` will have this `SubplotSpec` as
523
538
a parent.
524
539
525
540
Parameters
@@ -532,12 +547,12 @@ def subgridspec(self, nrows, ncols, **kwargs):
532
547
533
548
Returns
534
549
-------
535
- gridspec : `.GridSpec `
550
+ gridspec : `.GridSpecFromSubplotSpec `
536
551
537
552
Other Parameters
538
553
----------------
539
554
**kwargs
540
- All other parameters are passed to `.GridSpec `.
555
+ All other parameters are passed to `.GridSpecFromSubplotSpec `.
541
556
542
557
See Also
543
558
--------
@@ -555,5 +570,4 @@ def subgridspec(self, nrows, ncols, **kwargs):
555
570
for i in range(3):
556
571
fig.add_subplot(gssub[0, i])
557
572
"""
558
-
559
573
return GridSpecFromSubplotSpec (nrows , ncols , self , ** kwargs )
0 commit comments