Skip to content

Commit 8cf4cd4

Browse files
committed
DOC: Add sections to rcParams documentation
1 parent 1017fe1 commit 8cf4cd4

File tree

3 files changed

+108
-13
lines changed

3 files changed

+108
-13
lines changed

doc/sphinxext/rcparams.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@ def run(self):
1919
self.state.document.settings.env.note_dependency(__file__)
2020
self.state.document.settings.env.note_dependency(rcsetup.__file__)
2121
lines = []
22-
for param in rcsetup._params:
23-
if param.name[0] == '_':
24-
continue
25-
lines += [
26-
f'.. _rcparam_{param.name.replace(".", "_")}:',
27-
'',
28-
f'{param.name}: ``{param.default!r}``',
29-
f' {param.description if param.description else "*no description*"}'
30-
]
22+
for elem in rcsetup._DEFINITION:
23+
if isinstance(elem, (rcsetup._Section, rcsetup._Subsection)):
24+
title_char = '-' if isinstance(elem, rcsetup._Section) else '~'
25+
lines += [
26+
'',
27+
elem.title,
28+
title_char * len(elem.title),
29+
'',
30+
elem.description or "",
31+
'',
32+
]
33+
elif isinstance(elem, rcsetup._Param):
34+
if elem.name[0] == '_':
35+
continue
36+
lines += [
37+
f'.. _rcparam_{elem.name.replace(".", "_")}:',
38+
'',
39+
f'{elem.name}: ``{elem.default!r}``',
40+
f' {elem.description if elem.description else "*no description*"}'
41+
]
3142
self.state_machine.insert_input(lines, 'rcParams table')
3243
return []
3344

lib/matplotlib/rcsetup.py

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,33 @@ class _Param:
14501450
description: str = None
14511451

14521452

1453-
_params = [
1453+
@dataclass
1454+
class _Section:
1455+
title: str
1456+
description: str = None
1457+
1458+
1459+
@dataclass
1460+
class _Subsection:
1461+
title: str
1462+
description: str = None
1463+
1464+
1465+
# Definition of all rcParams. This is currently only used to generate the documentation.
1466+
#
1467+
# Actual runtime values do still come from the historic sources:
1468+
# - available parameters and defaults: lib/matplotlib/mpl-data/matplotlibrc
1469+
# - validators: _validators, see above
1470+
#
1471+
# The structure and format of this definition is not fixed and may change in the future.
1472+
# It's a work-in-progress state towards a consistent and more structured definition of
1473+
# rcParams that can be used both for documentation and runtime. The goal is to
1474+
# eventually eliminate the old sources of defaults and validators and have this be the
1475+
# single source of truth.
1476+
#
1477+
# In the transition phase, consistency is ensured via tests.
1478+
_DEFINITION = [
1479+
_Section("Backends"),
14541480
_Param(
14551481
"webagg.port",
14561482
default=8988,
@@ -1507,6 +1533,11 @@ class _Param:
15071533
validator=validate_string,
15081534
description="a pytz timezone string, e.g., US/Central or Europe/Paris"
15091535
),
1536+
_Section(
1537+
"Lines",
1538+
description="Default properties for line objects, such as those returned by "
1539+
"plot()."
1540+
),
15101541
_Param(
15111542
"lines.linewidth",
15121543
default=1.5,
@@ -1627,6 +1658,7 @@ class _Param:
16271658
"solely to allow old test images to remain unchanged. Set to False "
16281659
"to obtain the previous behavior."
16291660
),
1661+
_Section("Patches"),
16301662
_Param(
16311663
"patch.linewidth",
16321664
default=1.0,
@@ -1661,8 +1693,10 @@ class _Param:
16611693
validator=validate_bool,
16621694
description="render patches in antialiased (no jaggies)"
16631695
),
1696+
_Section("Hatches"),
16641697
_Param("hatch.color", "edge", _validate_color_or_edge),
16651698
_Param("hatch.linewidth", 1.0, validate_float),
1699+
_Section("Boxplot"),
16661700
_Param("boxplot.notch", False, validate_bool),
16671701
_Param("boxplot.vertical", True, validate_bool),
16681702
_Param("boxplot.whiskers", 1.5, validate_whiskers),
@@ -1700,6 +1734,13 @@ class _Param:
17001734
_Param("boxplot.meanprops.markersize", 6.0, validate_float),
17011735
_Param("boxplot.meanprops.linestyle", "--", _validate_linestyle),
17021736
_Param("boxplot.meanprops.linewidth", 1.0, validate_float),
1737+
_Section(
1738+
"Font",
1739+
description="The font properties used by `text.Text` "
1740+
"See https://matplotlib.org/stable/api/font_manager_api.html for "
1741+
"more information on font properties. The 6 font properties used "
1742+
"for font matching are given below with their default values."
1743+
),
17031744
_Param("font.family", ["sans-serif"], validate_stringlist),
17041745
_Param("font.style", "normal", validate_string),
17051746
_Param("font.variant", "normal", validate_string),
@@ -1755,6 +1796,7 @@ class _Param:
17551796
"appended to all font selections. This ensures that there will "
17561797
"always be a glyph displayed."
17571798
),
1799+
_Section("Text properties"),
17581800
_Param(
17591801
"text.color",
17601802
default="black",
@@ -1808,6 +1850,7 @@ class _Param:
18081850
description="Use mathtext if there is an even number of unescaped dollar signs."
18091851

18101852
),
1853+
_Section("Mathtext and LaTeX"),
18111854
_Param(
18121855
"text.usetex",
18131856
default=False,
@@ -1866,6 +1909,7 @@ class _Param:
18661909
'names, including the special name "regular" for the same font '
18671910
'used in regular text.',
18681911
),
1912+
_Section("Axes"),
18691913
_Param(
18701914
"axes.facecolor",
18711915
default="white",
@@ -2077,12 +2121,14 @@ class _Param:
20772121
'"round_numbers", after application of margins, axis limits are '
20782122
'further expanded to the nearest "round" number.',
20792123
),
2124+
_Subsection("Polar Axes"),
20802125
_Param(
20812126
"polaraxes.grid",
20822127
default=True,
20832128
validator=validate_bool,
20842129
description="display grid on polar axes"
20852130
),
2131+
_Subsection("3D Axes"),
20862132
_Param(
20872133
"axes3d.grid",
20882134
default=True,
@@ -2143,6 +2189,7 @@ class _Param:
21432189
description="trackball border width, in units of the Axes bbox (only for "
21442190
"'sphere' and 'arcball' style)"
21452191
),
2192+
_Section("Axis"),
21462193
_Param(
21472194
"xaxis.labellocation",
21482195
default="center",
@@ -2155,6 +2202,14 @@ class _Param:
21552202
validator=["bottom", "center", "top"],
21562203
description="alignment of the yaxis label: {bottom, top, center}"
21572204
),
2205+
_Section(
2206+
"Dates",
2207+
description="Default properties for date tick labels. These are used by the "
2208+
"`.AutoDateFormatter` when the appropriate time unit is detected."
2209+
"See "
2210+
"https://matplotlib.org/stable/api/dates_api.html#date-formatters "
2211+
"for more information."
2212+
),
21582213
_Param("date.autoformatter.year", "%Y", validate_string),
21592214
_Param("date.autoformatter.month", "%Y-%m", validate_string),
21602215
_Param("date.autoformatter.day", "%Y-%m-%d", validate_string),
@@ -2180,6 +2235,7 @@ class _Param:
21802235
validator=validate_bool,
21812236
description="For auto converter whether to use interval_multiples"
21822237
),
2238+
_Section("Ticks"),
21832239
_Param(
21842240
"xtick.top",
21852241
default=False,
@@ -2430,6 +2486,7 @@ class _Param:
24302486
["center", "top", "bottom", "baseline", "center_baseline"],
24312487
description="alignment of yticks"
24322488
),
2489+
_Section("Grid"),
24332490
_Param(
24342491
"grid.color",
24352492
default="#b0b0b0",
@@ -2502,6 +2559,7 @@ class _Param:
25022559
validator=validate_float_or_None,
25032560
description="If None defaults to grid.alpha"
25042561
),
2562+
_Section("Legend"),
25052563
_Param(
25062564
"legend.loc",
25072565
default="best",
@@ -2626,6 +2684,7 @@ class _Param:
26262684
default=2.0,
26272685
validator=validate_float, description="column separation"
26282686
),
2687+
_Section("Figure"),
26292688
_Param(
26302689
"figure.titlesize",
26312690
default="large",
@@ -2777,6 +2836,7 @@ class _Param:
27772836
"figure.subplot.wspace) as constrained_layout already takes "
27782837
"surrounding texts (titles, labels, # ticklabels) into account."
27792838
),
2839+
_Section("Images"),
27802840
_Param(
27812841
"image.aspect",
27822842
default="equal",
@@ -2825,6 +2885,7 @@ class _Param:
28252885
"single composite image before saving a figure as a vector "
28262886
"graphics file, such as a PDF."
28272887
),
2888+
_Section("Contour plots"),
28282889
_Param(
28292890
"contour.negative_linestyle",
28302891
default="dashed",
@@ -2849,18 +2910,21 @@ class _Param:
28492910
validator=["mpl2005", "mpl2014", "serial", "threaded"],
28502911
description="{mpl2005, mpl2014, serial, threaded}"
28512912
),
2913+
_Section("Errorbar plots"),
28522914
_Param(
28532915
"errorbar.capsize",
28542916
default=0.0,
28552917
validator=validate_float,
28562918
description="length of end cap on error bars in pixels"
28572919
),
2920+
_Section("Histogram plots"),
28582921
_Param(
28592922
"hist.bins",
28602923
default=10,
28612924
validator=validate_hist_bins,
28622925
description="The default number of histogram bins or 'auto'."
28632926
),
2927+
_Section("Scatter plots"),
28642928
_Param(
28652929
"scatter.marker",
28662930
default="o",
@@ -2873,6 +2937,7 @@ class _Param:
28732937
validator=validate_string,
28742938
description="The default edge colors for scatter plots."
28752939
),
2940+
_Section("AGG rendering"),
28762941
_Param(
28772942
"agg.path.chunksize",
28782943
default=0,
@@ -2883,6 +2948,7 @@ class _Param:
28832948
"cause minor artifacts, though. A value of 20000 is probably a "
28842949
"good starting point."
28852950
),
2951+
_Section("Paths"),
28862952
_Param(
28872953
"path.simplify",
28882954
default=True,
@@ -2922,6 +2988,7 @@ class _Param:
29222988
default=[],
29232989
validator=validate_anylist
29242990
),
2991+
_Section("Saving figures"),
29252992
_Param(
29262993
"savefig.dpi",
29272994
default="figure",
@@ -2980,19 +3047,22 @@ class _Param:
29803047
validator=["landscape", "portrait"],
29813048
description="orientation of saved figure, for PostScript output only"
29823049
),
3050+
_Subsection("Mac OSX backend parameters"),
29833051
_Param(
29843052
"macosx.window_mode",
29853053
default="system",
29863054
validator=["system", "tab", "window"],
29873055
description="How to open new figures (system, tab, window) system uses "
29883056
"the MacOS system preferences"
29893057
),
3058+
_Subsection("Tk backend parameters"),
29903059
_Param(
29913060
"tk.window_focus",
29923061
default=False,
29933062
validator=validate_bool,
29943063
description="Maintain shell focus for TkAgg"
29953064
),
3065+
_Subsection("PS backend parameters"),
29963066
_Param(
29973067
"ps.papersize",
29983068
default="letter",
@@ -3028,6 +3098,7 @@ class _Param:
30283098
validator=validate_fonttype,
30293099
description="Output Type 3 (Type3) or Type 42 (TrueType)"
30303100
),
3101+
_Subsection("PDF backend parameters"),
30313102
_Param(
30323103
"pdf.compression",
30333104
default=6,
@@ -3050,6 +3121,7 @@ class _Param:
30503121
default=False,
30513122
validator=validate_bool
30523123
),
3124+
_Subsection("SVG backend parameters"),
30533125
_Param(
30543126
"svg.image_inline",
30553127
default=True,
@@ -3079,6 +3151,7 @@ class _Param:
30793151
description="If not None, use this string as the value for the `id` attribute "
30803152
"in the top <svg> tag"
30813153
),
3154+
_Subsection("PGF parameters"),
30823155
_Param(
30833156
"pgf.rcfonts",
30843157
default=True,
@@ -3095,12 +3168,18 @@ class _Param:
30953168
default="xelatex",
30963169
validator=["xelatex", "lualatex", "pdflatex"]
30973170
),
3171+
_Subsection("Docstring parameters"),
30983172
_Param(
30993173
"docstring.hardcopy",
31003174
default=False,
31013175
validator=validate_bool,
31023176
description="set this when you want to generate hardcopy docstring"
31033177
),
3178+
_Section(
3179+
"Interactive keymaps",
3180+
description="Default key mappings for interactive navigation. See "
3181+
":ref:`key-event-handling`."
3182+
),
31043183
_Param(
31053184
"keymap.fullscreen",
31063185
default=["f", "ctrl+f"],
@@ -3187,6 +3266,7 @@ class _Param:
31873266
validator=validate_stringlist,
31883267
description="copy figure to clipboard"
31893268
),
3269+
_Section("Animation"),
31903270
_Param(
31913271
"animation.html",
31923272
default="none",
@@ -3260,3 +3340,7 @@ class _Param:
32603340
),
32613341
_Param("backend", None, validate_backend),
32623342
]
3343+
3344+
3345+
def _params_list():
3346+
return [elem for elem in _DEFINITION if isinstance(elem, _Param)]

lib/matplotlib/tests/test_rcparams.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,17 @@ def test_rc_aliases(group, option, alias, value):
676676

677677

678678
def test_all_params_defined_as_code():
679-
assert set(p.name for p in rcsetup._params) == set(mpl.rcParams.keys())
679+
assert set(p.name for p in rcsetup._params_list()) == set(mpl.rcParams.keys())
680680

681681

682682
def test_validators_defined_as_code():
683-
for param in rcsetup._params:
683+
for param in rcsetup._params_list():
684684
validator = rcsetup._convert_validator_spec(param.name, param.validator)
685685
assert validator == rcsetup._validators[param.name]
686686

687687

688688
def test_defaults_as_code():
689-
for param in rcsetup._params:
689+
for param in rcsetup._params_list():
690690
if param.name == 'backend':
691691
# backend has special handling and no meaningful default
692692
continue

0 commit comments

Comments
 (0)