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

Skip to content

Commit a1b4679

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 153b463 commit a1b4679

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-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 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: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class FigureBase(Artist):
181181
Base class for `.figure.Figure` and `.figure.SubFigure` containing the
182182
methods that add artists to the figure or subfigure, create Axes, etc.
183183
"""
184-
def __init__(self):
184+
def __init__(self, **kwargs):
185185
super().__init__()
186186
# remove the non-figure artist _axes property
187187
# as it makes no sense for a figure to be _in_ an axes
@@ -214,6 +214,7 @@ def __init__(self):
214214
self.subfigs = []
215215
self.stale = True
216216
self.suppressComposite = None
217+
self.set(**kwargs)
217218

218219
def _get_draw_artists(self, renderer):
219220
"""Also runs apply_aspect"""
@@ -1939,7 +1940,8 @@ def __init__(self, parent, subplotspec, *,
19391940
facecolor=None,
19401941
edgecolor=None,
19411942
linewidth=0.0,
1942-
frameon=None):
1943+
frameon=None,
1944+
**kwargs):
19431945
"""
19441946
Parameters
19451947
----------
@@ -1963,8 +1965,14 @@ def __init__(self, parent, subplotspec, *,
19631965
19641966
frameon : bool, default: :rc:`figure.frameon`
19651967
If ``False``, suppress drawing the figure background patch.
1968+
1969+
Other Parameters
1970+
----------------
1971+
**kwargs : `.Artist` properties, optional
1972+
1973+
%(Artist_kwdoc)s
19661974
"""
1967-
super().__init__()
1975+
super().__init__(**kwargs)
19681976
if facecolor is None:
19691977
facecolor = mpl.rcParams['figure.facecolor']
19701978
if edgecolor is None:
@@ -2147,6 +2155,7 @@ def __init__(self,
21472155
subplotpars=None, # rc figure.subplot.*
21482156
tight_layout=None, # rc figure.autolayout
21492157
constrained_layout=None, # rc figure.constrained_layout.use
2158+
**kwargs
21502159
):
21512160
"""
21522161
Parameters
@@ -2188,8 +2197,14 @@ def __init__(self,
21882197
:doc:`/tutorials/intermediate/constrainedlayout_guide`
21892198
for examples. (Note: does not work with `add_subplot` or
21902199
`~.pyplot.subplot2grid`.)
2200+
2201+
Other Parameters
2202+
----------------
2203+
**kwargs : `.Artist` properties, optional
2204+
2205+
%(Artist_kwdoc)s
21912206
"""
2192-
super().__init__()
2207+
super().__init__(**kwargs)
21932208

21942209
self.callbacks = cbook.CallbackRegistry()
21952210
# 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
@@ -1068,3 +1068,11 @@ def test_add_axes_kwargs():
10681068
assert ax1.name == 'rectilinear'
10691069
assert ax1 is not ax
10701070
plt.close()
1071+
1072+
1073+
def test_kwargs_pass():
1074+
fig = Figure(label='whole Figure')
1075+
sub_fig = fig.subfigures(1, 1, label='sub figure')
1076+
1077+
assert fig.get_label() == 'whole Figure'
1078+
assert sub_fig.get_label() == 'sub figure'

0 commit comments

Comments
 (0)