Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74079d0

Browse files
tacaswellQuLogic
andcommitted
ENH: pass extra kwrags in FigureBase, SubFigure, Figure to set
Consistent with other artists and allows properties controlled by `set_XYZ` to be set at init time. Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent ec126b0 commit 74079d0

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
Figure init passes keyword arguments through to set
3+
---------------------------------------------------
4+
5+
Similar to many other sub-classes of `~.Artist`, `~.FigureBase`, `~.SubFigure`,
6+
and `~.Figure` will now pass any additional keyword arguments to `~.Artist.set`
7+
to allow properties of the newly created object to be set at init time. For
8+
example ::
9+
10+
from matplotlib.figure import Figure
11+
fig = Figure(label='my figure')

lib/matplotlib/figure.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class FigureBase(Artist):
184184
Base class for `.figure.Figure` and `.figure.SubFigure` containing the
185185
methods that add artists to the figure or subfigure, create Axes, etc.
186186
"""
187-
def __init__(self):
187+
def __init__(self, **kwargs):
188188
super().__init__()
189189
# remove the non-figure artist _axes property
190190
# as it makes no sense for a figure to be _in_ an axes
@@ -217,6 +217,7 @@ def __init__(self):
217217
self.subfigs = []
218218
self.stale = True
219219
self.suppressComposite = None
220+
self.set(**kwargs)
220221

221222
def _get_draw_artists(self, renderer):
222223
"""Also runs apply_aspect"""
@@ -1923,6 +1924,7 @@ def _set_artist_props(self, a):
19231924
a.set_transform(self.transSubfigure)
19241925

19251926

1927+
@docstring.interpd
19261928
class SubFigure(FigureBase):
19271929
"""
19281930
Logical figure that can be placed inside a figure.
@@ -1945,7 +1947,8 @@ def __init__(self, parent, subplotspec, *,
19451947
facecolor=None,
19461948
edgecolor=None,
19471949
linewidth=0.0,
1948-
frameon=None):
1950+
frameon=None,
1951+
**kwargs):
19491952
"""
19501953
Parameters
19511954
----------
@@ -1969,8 +1972,14 @@ def __init__(self, parent, subplotspec, *,
19691972
19701973
frameon : bool, default: :rc:`figure.frameon`
19711974
If ``False``, suppress drawing the figure background patch.
1975+
1976+
Other Parameters
1977+
----------------
1978+
**kwargs : `.SubFigure` properties, optional
1979+
1980+
%(SubFigure:kwdoc)s
19721981
"""
1973-
super().__init__()
1982+
super().__init__(**kwargs)
19741983
if facecolor is None:
19751984
facecolor = mpl.rcParams['figure.facecolor']
19761985
if edgecolor is None:
@@ -2114,6 +2123,7 @@ def draw(self, renderer):
21142123
self.stale = False
21152124

21162125

2126+
@docstring.interpd
21172127
class Figure(FigureBase):
21182128
"""
21192129
The top level container for all the plot elements.
@@ -2154,6 +2164,7 @@ def __init__(self,
21542164
subplotpars=None, # rc figure.subplot.*
21552165
tight_layout=None, # rc figure.autolayout
21562166
constrained_layout=None, # rc figure.constrained_layout.use
2167+
**kwargs
21572168
):
21582169
"""
21592170
Parameters
@@ -2195,8 +2206,14 @@ def __init__(self,
21952206
:doc:`/tutorials/intermediate/constrainedlayout_guide`
21962207
for examples. (Note: does not work with `add_subplot` or
21972208
`~.pyplot.subplot2grid`.)
2209+
2210+
Other Parameters
2211+
----------------
2212+
**kwargs : `.Figure` properties, optional
2213+
2214+
%(Figure:kwdoc)s
21982215
"""
2199-
super().__init__()
2216+
super().__init__(**kwargs)
22002217

22012218
self.callbacks = cbook.CallbackRegistry()
22022219
# Callbacks traditionally associated with the canvas (and exposed with

lib/matplotlib/tests/test_figure.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,3 +1110,11 @@ def test_waitforbuttonpress(recwarn): # recwarn undoes warn filters at exit.
11101110
assert fig.waitforbuttonpress() is True
11111111
Timer(.1, fig.canvas.button_press_event, (0, 0, 1)).start()
11121112
assert fig.waitforbuttonpress() is False
1113+
1114+
1115+
def test_kwargs_pass():
1116+
fig = Figure(label='whole Figure')
1117+
sub_fig = fig.subfigures(1, 1, label='sub figure')
1118+
1119+
assert fig.get_label() == 'whole Figure'
1120+
assert sub_fig.get_label() == 'sub figure'

0 commit comments

Comments
 (0)