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

Skip to content

Add rcparam for figure label size and weight #22566

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

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/users/next_whats_new/figure_label_rcparams.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Allow setting figure label size and weight globally and separately from title
-----------------------------------------------------------------------------

The figure labels, ``Figure.supxlabel`` and ``Figure.supylabel``, size and
weight can be set separately from the figure title. Use :rc:`figure.labelsize`
and :rc:`figure.labelweight`.

Note that if you have locally changed :rc:`figure.titlesize` or
:rc:`figure.titleweight`, you must now also change the introduced parameters
for a consistent result.
27 changes: 15 additions & 12 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ def _suplabels(self, t, info, **kwargs):
verticalalignment, va : {'top', 'center', 'bottom', 'baseline'}, \
default: %(va)s
The vertical alignment of the text relative to (*x*, *y*).
fontsize, size : default: :rc:`figure.titlesize`
fontsize, size : default: :rc:`figure.%(rc)ssize`
The font size of the text. See `.Text.set_size` for possible
values.
fontweight, weight : default: :rc:`figure.titleweight`
fontweight, weight : default: :rc:`figure.%(rc)sweight`
The font weight of the text. See `.Text.set_weight` for possible
values.

Expand All @@ -331,8 +331,8 @@ def _suplabels(self, t, info, **kwargs):
fontproperties : None or dict, optional
A dict of font properties. If *fontproperties* is given the
default values for font size and weight are taken from the
`.FontProperties` defaults. :rc:`figure.titlesize` and
:rc:`figure.titleweight` are ignored in this case.
`.FontProperties` defaults. :rc:`figure.%(rc)ssize` and
:rc:`figure.%(rc)sweight` are ignored in this case.

**kwargs
Additional kwargs are `matplotlib.text.Text` properties.
Expand Down Expand Up @@ -360,9 +360,9 @@ def _suplabels(self, t, info, **kwargs):

if 'fontproperties' not in kwargs:
if 'fontsize' not in kwargs and 'size' not in kwargs:
kwargs['size'] = mpl.rcParams['figure.titlesize']
kwargs['size'] = mpl.rcParams[info['size']]
if 'fontweight' not in kwargs and 'weight' not in kwargs:
kwargs['weight'] = mpl.rcParams['figure.titleweight']
kwargs['weight'] = mpl.rcParams[info['weight']]

sup = self.text(x, y, t, **kwargs)
if suplab is not None:
Expand All @@ -378,31 +378,34 @@ def _suplabels(self, t, info, **kwargs):
return suplab

@_docstring.Substitution(x0=0.5, y0=0.98, name='suptitle', ha='center',
va='top')
va='top', rc='title')
@_docstring.copy(_suplabels)
def suptitle(self, t, **kwargs):
# docstring from _suplabels...
info = {'name': '_suptitle', 'x0': 0.5, 'y0': 0.98,
'ha': 'center', 'va': 'top', 'rotation': 0}
'ha': 'center', 'va': 'top', 'rotation': 0,
'size': 'figure.titlesize', 'weight': 'figure.titleweight'}
return self._suplabels(t, info, **kwargs)

@_docstring.Substitution(x0=0.5, y0=0.01, name='supxlabel', ha='center',
va='bottom')
va='bottom', rc='label')
@_docstring.copy(_suplabels)
def supxlabel(self, t, **kwargs):
# docstring from _suplabels...
info = {'name': '_supxlabel', 'x0': 0.5, 'y0': 0.01,
'ha': 'center', 'va': 'bottom', 'rotation': 0}
'ha': 'center', 'va': 'bottom', 'rotation': 0,
'size': 'figure.labelsize', 'weight': 'figure.labelweight'}
return self._suplabels(t, info, **kwargs)

@_docstring.Substitution(x0=0.02, y0=0.5, name='supylabel', ha='left',
va='center')
va='center', rc='label')
@_docstring.copy(_suplabels)
def supylabel(self, t, **kwargs):
# docstring from _suplabels...
info = {'name': '_supylabel', 'x0': 0.02, 'y0': 0.5,
'ha': 'left', 'va': 'center', 'rotation': 'vertical',
'rotation_mode': 'anchor'}
'rotation_mode': 'anchor', 'size': 'figure.labelsize',
'weight': 'figure.labelweight'}
return self._suplabels(t, info, **kwargs)

def get_edgecolor(self):
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@
## See https://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure
#figure.titlesize: large # size of the figure title (``Figure.suptitle()``)
#figure.titleweight: normal # weight of the figure title
#figure.labelsize: large # size of the figure label (``Figure.sup[x|y]label()``)
#figure.labelweight: normal # weight of the figure label
#figure.figsize: 6.4, 4.8 # figure size in inches
#figure.dpi: 100 # figure dots per inch
#figure.facecolor: white # figure face color
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/mpl-data/stylelib/classic.mplstyle
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ legend.edgecolor : inherit # legend edge color (when 'inherit' uses axes.e
# See https://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure
figure.titlesize : medium # size of the figure title
figure.titleweight : normal # weight of the figure title
figure.labelsize: medium # size of the figure label
figure.labelweight: normal # weight of the figure label
figure.figsize : 8, 6 # figure size in inches
figure.dpi : 80 # figure dots per inch
figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,10 @@ def _convert_validator_spec(key, conv):
"figure.titlesize": validate_fontsize,
"figure.titleweight": validate_fontweight,

# figure labels
"figure.labelsize": validate_fontsize,
"figure.labelweight": validate_fontweight,

# figure size in inches: width by height
"figure.figsize": _listify_validator(validate_float, n=2),
"figure.dpi": validate_float,
Expand Down
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,20 @@ def test_kwargs_pass():
assert sub_fig.get_label() == 'sub figure'


@check_figures_equal(extensions=["png"])
def test_rcparams(fig_test, fig_ref):
fig_ref.supxlabel("xlabel", weight='bold', size=15)
fig_ref.supylabel("ylabel", weight='bold', size=15)
fig_ref.suptitle("Title", weight='light', size=20)
with mpl.rc_context({'figure.labelweight': 'bold',
'figure.labelsize': 15,
'figure.titleweight': 'light',
'figure.titlesize': 20}):
fig_test.supxlabel("xlabel")
fig_test.supylabel("ylabel")
fig_test.suptitle("Title")


def test_deepcopy():
fig1, ax = plt.subplots()
ax.plot([0, 1], [2, 3])
Expand Down