Skip to content

Commit b5b9c0a

Browse files
committed
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.
1 parent ec126b0 commit b5b9c0a

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
Figure init passes 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

+19-4
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"""
@@ -1945,7 +1946,8 @@ def __init__(self, parent, subplotspec, *,
19451946
facecolor=None,
19461947
edgecolor=None,
19471948
linewidth=0.0,
1948-
frameon=None):
1949+
frameon=None,
1950+
**kwargs):
19491951
"""
19501952
Parameters
19511953
----------
@@ -1969,8 +1971,14 @@ def __init__(self, parent, subplotspec, *,
19691971
19701972
frameon : bool, default: :rc:`figure.frameon`
19711973
If ``False``, suppress drawing the figure background patch.
1974+
1975+
Other Parameters
1976+
----------------
1977+
**kwargs : `.Artist` properties, optional
1978+
1979+
%(Artist_kwdoc)s
19721980
"""
1973-
super().__init__()
1981+
super().__init__(**kwargs)
19741982
if facecolor is None:
19751983
facecolor = mpl.rcParams['figure.facecolor']
19761984
if edgecolor is None:
@@ -2154,6 +2162,7 @@ def __init__(self,
21542162
subplotpars=None, # rc figure.subplot.*
21552163
tight_layout=None, # rc figure.autolayout
21562164
constrained_layout=None, # rc figure.constrained_layout.use
2165+
**kwargs
21572166
):
21582167
"""
21592168
Parameters
@@ -2195,8 +2204,14 @@ def __init__(self,
21952204
:doc:`/tutorials/intermediate/constrainedlayout_guide`
21962205
for examples. (Note: does not work with `add_subplot` or
21972206
`~.pyplot.subplot2grid`.)
2207+
2208+
Other Parameters
2209+
----------------
2210+
**kwargs : `.Artist` properties, optional
2211+
2212+
%(Artist_kwdoc)s
21982213
"""
2199-
super().__init__()
2214+
super().__init__(**kwargs)
22002215

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

lib/matplotlib/tests/test_figure.py

+8
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)