-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Resolving: Add setter/getter methods for all keyword parameters to Figure.__init__ (Issue: #24617) #27531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Resolving: Add setter/getter methods for all keyword parameters to Figure.__init__ (Issue: #24617) #27531
Changes from all commits
62c88bf
77cace7
50239bf
573a98d
089c935
fac1a90
cecde08
1622057
80831c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff l 8000 ine change | ||||
---|---|---|---|---|---|---|
|
@@ -10,6 +10,9 @@ | |||||
`SubFigure`) with `Figure.add_subfigure` or `Figure.subfigures` methods | ||||||
(provisional API v3.4). | ||||||
|
||||||
`SubplotParams` | ||||||
Control the default spacing between subplots. | ||||||
|
||||||
Figures are typically created using pyplot methods `~.pyplot.figure`, | ||||||
`~.pyplot.subplots`, and `~.pyplot.subplot_mosaic`. | ||||||
|
||||||
|
@@ -48,7 +51,7 @@ | |||||
import matplotlib.image as mimage | ||||||
|
||||||
from matplotlib.axes import Axes | ||||||
from matplotlib.gridspec import GridSpec, SubplotParams | ||||||
from matplotlib.gridspec import GridSpec | ||||||
from matplotlib.layout_engine import ( | ||||||
ConstrainedLayoutEngine, TightLayoutEngine, LayoutEngine, | ||||||
PlaceHolderLayoutEngine | ||||||
|
@@ -115,6 +118,66 @@ def __setstate__(self, state): | |||||
self._counter = itertools.count(next_counter) | ||||||
|
||||||
|
||||||
class SubplotParams: | ||||||
""" | ||||||
A class to hold the parameters for a subplot. | ||||||
""" | ||||||
|
||||||
def __init__(self, left=None, bottom=None, right=None, top=None, | ||||||
wspace=None, hspace=None): | ||||||
""" | ||||||
Defaults are given by :rc:`figure.subplot.[name]`. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
left : float | ||||||
The position of the left edge of the subplots, | ||||||
as a fraction of the figure width. | ||||||
right : float | ||||||
The position of the right edge of the subplots, | ||||||
as a fraction of the figure width. | ||||||
bottom : float | ||||||
The position of the bottom edge of the subplots, | ||||||
as a fraction of the figure height. | ||||||
top : float | ||||||
The position of the top edge of the subplots, | ||||||
as a fraction of the figure height. | ||||||
wspace : float | ||||||
The width of the padding between subplots, | ||||||
as a fraction of the average Axes width. | ||||||
hspace : float | ||||||
The height of the padding between subplots, | ||||||
as a fraction of the average Axes height. | ||||||
""" | ||||||
for key in ["left", "bottom", "right", "top", "wspace", "hspace"]: | ||||||
setattr(self, key, mpl.rcParams[f"figure.subplot.{key}"]) | ||||||
self.update(left, bottom, right, top, wspace, hspace) | ||||||
|
||||||
def update(self, left=None, bottom=None, right=None, top=None, | ||||||
wspace=None, hspace=None): | ||||||
""" | ||||||
Update the dimensions of the passed parameters. *None* means unchanged. | ||||||
""" | ||||||
if ((left if left is not None else self.left) | ||||||
>= (right if right is not None else self.right)): | ||||||
raise ValueError('left cannot be >= right') | ||||||
if ((bottom if bottom is not None else self.bottom) | ||||||
>= (top if top is not None else self.top)): | ||||||
raise ValueError('bottom cannot be >= top') | ||||||
if left is not None: | ||||||
self.left = left | ||||||
if right is not None: | ||||||
self.right = right | ||||||
if bottom is not None: | ||||||
self.bottom = bottom | ||||||
if top is not None: | ||||||
self.top = top | ||||||
if wspace is not None: | ||||||
self.wspace = wspace | ||||||
if hspace is not None: | ||||||
self.hspace = hspace | ||||||
|
||||||
|
||||||
class FigureBase(Artist): | ||||||
""" | ||||||
Base class for `.Figure` and `.SubFigure` containing the methods that add | ||||||
|
@@ -1291,6 +1354,65 @@ def subplots_adjust(self, left=None, bottom=None, right=None, top=None, | |||||
ax._set_position(ax.get_subplotspec().get_position(self)) | ||||||
self.stale = True | ||||||
|
||||||
def set_subplotpars(self, subplotparams={}): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default values should not be mutable objects like a dictionary. You should set it to
Suggested change
|
||||||
""" | ||||||
Set the subplot layout parameters. | ||||||
Accepts either a `.SubplotParams` object, from which the relevant | ||||||
parameters are copied, or a dictionary of subplot layout parameters. | ||||||
If a dictionary is provided, this function is a convenience wrapper for | ||||||
`matplotlib.figure.Figure.subplots_adjust` | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
subplotparams : `~matplotlib.figure.SubplotParams` or dict with keys | ||||||
"left", "bottom", "right", 'top", "wspace", "hspace"] , optional | ||||||
SubplotParams object to copy new subplot parameters from, or a dict | ||||||
of SubplotParams constructor arguments. | ||||||
By default, an empty dictionary is passed, which maintains the | ||||||
current state of the figure's `.SubplotParams` | ||||||
|
||||||
See Also | ||||||
-------- | ||||||
matplotlib.figure.Figure.subplots_adjust | ||||||
matplotlib.figure.Figure.get_subplotpars | ||||||
""" | ||||||
subplotparams_args = ["left", "bottom", "right", | ||||||
"top", "wspace", "hspace"] | ||||||
kwargs = {} | ||||||
if isinstance(subplotparams, SubplotParams): | ||||||
for key in subplotparams_args: | ||||||
kwargs[key] = getattr(subplotparams, key) | ||||||
elif isinstance(subplotparams, dict): | ||||||
for key in subplotparams.keys(): | ||||||
if key in subplotparams_args: | ||||||
kwargs[key] = subplotparams[key] | ||||||
else: | ||||||
_api.warn_external( | ||||||
f"'{key}' is not a valid key for set_subplotpars;" | ||||||
" this key was ignored.") | ||||||
Comment on lines
+1389
to
+1392
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a new function, this should be an error, not a warning. |
||||||
else: | ||||||
raise TypeError( | ||||||
"subplotpars must be a dictionary of keyword-argument pairs or" | ||||||
" an instance of SubplotParams()") | ||||||
if kwargs == {}: | ||||||
self.set_subplotpars(self.get_subplotpars()) | ||||||
Comment on lines
+1397
to
+1398
There was a problem hiding this comment. C 8000 hoose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what this is doing? |
||||||
self.subplots_adjust(**kwargs) | ||||||
|
||||||
def get_subplotpars(self): | ||||||
""" | ||||||
Return the `.SubplotParams` object associated with the Figure. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
`.SubplotParams` | ||||||
|
||||||
See Also | ||||||
-------- | ||||||
matplotlib.figure.Figure.subplots_adjust | ||||||
matplotlib.figure.Figure.get_subplotpars | ||||||
""" | ||||||
return self.subplotpars | ||||||
|
||||||
def align_xlabels(self, axs=None): | ||||||
""" | ||||||
Align the xlabels of subplots in the same subplot column if label | ||||||
|
@@ -2306,6 +2428,10 @@ def draw(self, renderer): | |||||
|
||||||
|
||||||
@_docstring.interpd | ||||||
@_api.define_aliases({ | ||||||
"size_inches": ["figsize"], | ||||||
"layout_engine": ["layout"] | ||||||
}) | ||||||
class Figure(FigureBase): | ||||||
""" | ||||||
The top level container for all the plot elements. | ||||||
|
@@ -2381,7 +2507,7 @@ def __init__(self, | |||||
frameon : bool, default: :rc:`figure.frameon` | ||||||
If ``False``, suppress drawing the figure background patch. | ||||||
|
||||||
subplotpars : `~matplotlib.gridspec.SubplotParams` | ||||||
subplotpars : `SubplotParams` | ||||||
Subplot parameters. If not given, the default subplot | ||||||
parameters :rc:`figure.subplot.*` are used. | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this need to come back?