Skip to content

Commit 6fc9624

Browse files
authored
Merge pull request #26634 from timhoffm/move-subplotparams
[MNT] Move SubplotParams from figure to gridspec
2 parents 469b96b + 219323a commit 6fc9624

File tree

7 files changed

+102
-99
lines changed

7 files changed

+102
-99
lines changed

doc/api/gridspec_api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Classes
1919
SubplotSpec
2020
GridSpecBase
2121
GridSpecFromSubplotSpec
22+
SubplotParams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``SubplotParams`` has been moved from ``matplotlib.figure`` to ``matplotlib.gridspec``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
It is still importable from ``matplotlib.figure``, so does not require any changes to
5+
existing code.

galleries/examples/subplots_axes_and_figures/auto_subplots_adjust.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ def on_draw(event):
8383
# - `matplotlib.transforms.BboxBase.union`
8484
# - `matplotlib.transforms.Transform.inverted`
8585
# - `matplotlib.figure.Figure.subplots_adjust`
86-
# - `matplotlib.figure.SubplotParams`
86+
# - `matplotlib.gridspec.SubplotParams`
8787
# - `matplotlib.backend_bases.FigureCanvasBase.mpl_connect`

lib/matplotlib/figure.py

+2-65
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
`SubFigure`) with `Figure.add_subfigure` or `Figure.subfigures` methods
1111
(provisional API v3.4).
1212
13-
`SubplotParams`
14-
Control the default spacing between subplots.
15-
1613
Figures are typically created using pyplot methods `~.pyplot.figure`,
1714
`~.pyplot.subplots`, and `~.pyplot.subplot_mosaic`.
1815
@@ -51,7 +48,7 @@
5148
import matplotlib.image as mimage
5249

5350
from matplotlib.axes import Axes
54-
from matplotlib.gridspec import GridSpec
51+
from matplotlib.gridspec import GridSpec, SubplotParams
5552
from matplotlib.layout_engine import (
5653
ConstrainedLayoutEngine, TightLayoutEngine, LayoutEngine,
5754
PlaceHolderLayoutEngine
@@ -118,66 +115,6 @@ def __setstate__(self, state):
118115
self._counter = itertools.count(next_counter)
119116

120117

121-
class SubplotParams:
122-
"""
123-
A class to hold the parameters for a subplot.
124-
"""
125-
126-
def __init__(self, left=None, bottom=None, right=None, top=None,
127-
wspace=None, hspace=None):
128-
"""
129-
Defaults are given by :rc:`figure.subplot.[name]`.
130-
131-
Parameters
132-
----------
133-
left : float
134-
The position of the left edge of the subplots,
135-
as a fraction of the figure width.
136-
right : float
137-
The position of the right edge of the subplots,
138-
as a fraction of the figure width.
139-
bottom : float
140-
The position of the bottom edge of the subplots,
141-
as a fraction of the figure height.
142-
top : float
143-
The position of the top edge of the subplots,
144-
as a fraction of the figure height.
145-
wspace : float
146-
The width of the padding between subplots,
147-
as a fraction of the average Axes width.
148-
hspace : float
149-
The height of the padding between subplots,
150-
as a fraction of the average Axes height.
151-
"""
152-
for key in ["left", "bottom", "right", "top", "wspace", "hspace"]:
153-
setattr(self, key, mpl.rcParams[f"figure.subplot.{key}"])
154-
self.update(left, bottom, right, top, wspace, hspace)
155-
156-
def update(self, left=None, bottom=None, right=None, top=None,
157-
wspace=None, hspace=None):
158-
"""
159-
Update the dimensions of the passed parameters. *None* means unchanged.
160-
"""
161-
if ((left if left is not None else self.left)
162-
>= (right if right is not None else self.right)):
163-
raise ValueError('left cannot be >= right')
164-
if ((bottom if bottom is not None else self.bottom)
165-
>= (top if top is not None else self.top)):
166-
raise ValueError('bottom cannot be >= top')
167-
if left is not None:
168-
self.left = left
169-
if right is not None:
170-
self.right = right
171-
if bottom is not None:
172-
self.bottom = bottom
173-
if top is not None:
174-
self.top = top
175-
if wspace is not None:
176-
self.wspace = wspace
177-
if hspace is not None:
178-
self.hspace = hspace
179-
180-
181118
class FigureBase(Artist):
182119
"""
183120
Base class for `.Figure` and `.SubFigure` containing the methods that add
@@ -2435,7 +2372,7 @@ def __init__(self,
24352372
frameon : bool, default: :rc:`figure.frameon`
24362373
If ``False``, suppress drawing the figure background patch.
24372374
2438-
subplotpars : `SubplotParams`
2375+
subplotpars : `~matplotlib.gridspec.SubplotParams`
24392376
Subplot parameters. If not given, the default subplot
24402377
parameters :rc:`figure.subplot.*` are used.
24412378

lib/matplotlib/figure.pyi

+1-27
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from matplotlib.backend_bases import (
1111
from matplotlib.colors import Colormap, Normalize
1212
from matplotlib.colorbar import Colorbar
1313
from matplotlib.cm import ScalarMappable
14-
from matplotlib.gridspec import GridSpec, SubplotSpec
14+
from matplotlib.gridspec import GridSpec, SubplotSpec, SubplotParams as SubplotParams
1515
from matplotlib.image import _ImageBase, FigureImage
1616
from matplotlib.layout_engine import LayoutEngine
1717
from matplotlib.legend import Legend
@@ -27,32 +27,6 @@ from collections.abc import Callable, Iterable
2727
from typing import Any, IO, Literal, overload
2828
from .typing import ColorType, HashableList
2929

30-
class SubplotParams:
31-
def __init__(
32-
self,
33-
left: float | None = ...,
34-
bottom: float | None = ...,
35-
right: float | None = ...,
36-
top: float | None = ...,
37-
wspace: float | None = ...,
38-
hspace: float | None = ...,
39-
) -> None: ...
40-
left: float
41-
right: float
42-
bottom: float
43-
top: float
44-
wspace: float
45-
hspace: float
46-
def update(
47-
self,
48-
left: float | None = ...,
49-
bottom: float | None = ...,
50-
right: float | None = ...,
51-
top: float | None = ...,
52-
wspace: float | None = ...,
53-
hspace: float | None = ...,
54-
) -> None: ...
55-
5630
class FigureBase(Artist):
5731
artists: list[Artist]
5832
lines: list[Line2D]

lib/matplotlib/gridspec.py

+65-5
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class GridSpec(GridSpecBase):
320320
A grid layout to place subplots within a figure.
321321
322322
The location of the grid cells is determined in a similar way to
323-
`~.figure.SubplotParams` using *left*, *right*, *top*, *bottom*, *wspace*
323+
`.SubplotParams` using *left*, *right*, *top*, *bottom*, *wspace*
324324
and *hspace*.
325325
326326
Indexing a GridSpec instance returns a `.SubplotSpec`.
@@ -424,7 +424,7 @@ def get_subplot_params(self, figure=None):
424424
if figure is None:
425425
kw = {k: mpl.rcParams["figure.subplot."+k]
426426
for k in self._AllowedKeys}
427-
subplotpars = mpl.figure.SubplotParams(**kw)
427+
subplotpars = SubplotParams(**kw)
428428
else:
429429
subplotpars = copy.copy(figure.subplotpars)
430430

@@ -517,9 +517,9 @@ def get_subplot_params(self, figure=None):
517517
figbox = self._subplot_spec.get_position(figure)
518518
left, bottom, right, top = figbox.extents
519519

520-
return mpl.figure.SubplotParams(left=left, right=right,
521-
bottom=bottom, top=top,
522-
wspace=wspace, hspace=hspace)
520+
return SubplotParams(left=left, right=right,
521+
bottom=bottom, top=top,
522+
wspace=wspace, hspace=hspace)
523523

524524
def get_topmost_subplotspec(self):
525525
"""
@@ -736,3 +736,63 @@ def subgridspec(self, nrows, ncols, **kwargs):
736736
fig.add_subplot(gssub[0, i])
737737
"""
738738
return GridSpecFromSubplotSpec(nrows, ncols, self, **kwargs)
739+
740+
741+
class SubplotParams:
742+
"""
743+
Parameters defining the positioning of a subplots grid in a figure.
744+
"""
745+
746+
def __init__(self, left=None, bottom=None, right=None, top=None,
747+
wspace=None, hspace=None):
748+
"""
749+
Defaults are given by :rc:`figure.subplot.[name]`.
750+
751+
Parameters
752+
----------
753+
left : float
754+
The position of the left edge of the subplots,
755+
as a fraction of the figure width.
756+
right : float
757+
The position of the right edge of the subplots,
758+
as a fraction of the figure width.
759+
bottom : float
760+
The position of the bottom edge of the subplots,
761+
as a fraction of the figure height.
762+
top : float
763+
The position of the top edge of the subplots,
764+
as a fraction of the figure height.
765+
wspace : float
766+
The width of the padding between subplots,
767+
as a fraction of the average Axes width.
768+
hspace : float
769+
The height of the padding between subplots,
770+
as a fraction of the average Axes height.
771+
"""
772+
for key in ["left", "bottom", "right", "top", "wspace", "hspace"]:
773+
setattr(self, key, mpl.rcParams[f"figure.subplot.{key}"])
774+
self.update(left, bottom, right, top, wspace, hspace)
775+
776+
def update(self, left=None, bottom=None, right=None, top=None,
777+
wspace=None, hspace=None):
778+
"""
779+
Update the dimensions of the passed parameters. *None* means unchanged.
780+
"""
781+
if ((left if left is not None else self.left)
782+
>= (right if right is not None else self.right)):
783+
raise ValueError('left cannot be >= right')
784+
if ((bottom if bottom is not None else self.bottom)
785+
>= (top if top is not None else self.top)):
786+
raise ValueError('bottom cannot be >= top')
787+
if left is not None:
788+
self.left = left
789+
if right is not None:
790+
self.right = right
791+
if bottom is not None:
792+
self.bottom = bottom
793+
if top is not None:
794+
self.top = top
795+
if wspace is not None:
796+
self.wspace = wspace
797+
if hspace is not None:
798+
self.hspace = hspace

lib/matplotlib/gridspec.pyi

+27-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import numpy as np
55

66
from matplotlib.axes import Axes, SubplotBase
77
from matplotlib.backend_bases import RendererBase
8-
from matplotlib.figure import Figure, SubplotParams
8+
from matplotlib.figure import Figure
99
from matplotlib.transforms import Bbox
1010

1111
class GridSpecBase:
@@ -132,3 +132,29 @@ class SubplotSpec:
132132
def subgridspec(
133133
self, nrows: int, ncols: int, **kwargs
134134
) -> GridSpecFromSubplotSpec: ...
135+
136+
class SubplotParams:
137+
def __init__(
138+
self,
139+
left: float | None = ...,
140+
bottom: float | None = ...,
141+
right: float | None = ...,
142+
top: float | None = ...,
143+
wspace: float | None = ...,
144+
hspace: float | None = ...,
145+
) -> None: ...
146+
left: float
147+
right: float
148+
bottom: float
149+
top: float
150+
wspace: float
151+
hspace: float
152+
def update(
153+
self,
154+
left: float | None = ...,
155+
bottom: float | None = ...,
156+
right: float | None = ...,
157+
top: float | None = ...,
158+
wspace: float | None = ...,
159+
hspace: float | None = ...,
160+
) -> None: ...

0 commit comments

Comments
 (0)