Skip to content

Commit 2a632f7

Browse files
authored
Merge pull request #23665 from anntzer/sd
Harmonize docstrings for boxstyle/connectionstyle/arrowstyle.
2 parents 1456f87 + 0dc472b commit 2a632f7

File tree

1 file changed

+93
-77
lines changed

1 file changed

+93
-77
lines changed

lib/matplotlib/patches.py

+93-77
Original file line numberDiff line numberDiff line change
@@ -2179,22 +2179,32 @@ def draw_bbox(bbox, renderer, color='k', trans=None):
21792179
r.draw(renderer)
21802180

21812181

2182-
def _simpleprint_styles(_styles):
2183-
"""
2184-
A helper function for the _Style class. Given the dictionary of
2185-
{stylename: styleclass}, return a string rep of the list of keys.
2186-
Used to update the documentation.
2187-
"""
2188-
return "[{}]".format("|".join(map(" '{}' ".format, _styles)))
2189-
2190-
21912182
class _Style:
21922183
"""
21932184
A base class for the Styles. It is meant to be a container class,
21942185
where actual styles are declared as subclass of it, and it
21952186
provides some helper functions.
21962187
"""
21972188

2189+
def __init_subclass__(cls):
2190+
# Automatically perform docstring interpolation on the subclasses:
2191+
# This allows listing the supported styles via
2192+
# - %(BoxStyle:table)s
2193+
# - %(ConnectionStyle:table)s
2194+
# - %(ArrowStyle:table)s
2195+
# and additionally adding .. ACCEPTS: blocks via
2196+
# - %(BoxStyle:table_and_accepts)s
2197+
# - %(ConnectionStyle:table_and_accepts)s
2198+
# - %(ArrowStyle:table_and_accepts)s
2199+
_docstring.interpd.update({
2200+
f"{cls.__name__}:table": cls.pprint_styles(),
2201+
f"{cls.__name__}:table_and_accepts": (
2202+
cls.pprint_styles()
2203+
+ "\n\n .. ACCEPTS: ["
2204+
+ "|".join(map(" '{}' ".format, cls._style_list))
2205+
+ "]")
2206+
})
2207+
21982208
def __new__(cls, stylename, **kwargs):
21992209
"""Return the instance of the subclass with the given style name."""
22002210
# The "class" should have the _style_list attribute, which is a mapping
@@ -2239,7 +2249,6 @@ def pprint_styles(cls):
22392249
*[' '.join(cell.ljust(cl) for cell, cl in zip(row, col_len))
22402250
for row in table[1:]],
22412251
table_formatstr,
2242-
'',
22432252
])
22442253
return textwrap.indent(rst_table, prefix=' ' * 4)
22452254

@@ -2260,6 +2269,7 @@ def _register_style(style_list, cls=None, *, name=None):
22602269
return cls
22612270

22622271

2272+
@_docstring.dedent_interpd
22632273
class BoxStyle(_Style):
22642274
"""
22652275
`BoxStyle` is a container class which defines several
@@ -2279,7 +2289,7 @@ class BoxStyle(_Style):
22792289
22802290
The following boxstyle classes are defined.
22812291
2282-
%(AvailableBoxstyles)s
2292+
%(BoxStyle:table)s
22832293
22842294
An instance of a boxstyle class is a callable object, with the signature ::
22852295
@@ -2637,6 +2647,7 @@ def __call__(self, x0, y0, width, height, mutation_size):
26372647
return Path(saw_vertices, codes)
26382648

26392649

2650+
@_docstring.dedent_interpd
26402651
class ConnectionStyle(_Style):
26412652
"""
26422653
`ConnectionStyle` is a container class which defines
@@ -2657,7 +2668,7 @@ class ConnectionStyle(_Style):
26572668
26582669
The following classes are defined
26592670
2660-
%(AvailableConnectorstyles)s
2671+
%(ConnectionStyle:table)s
26612672
26622673
An instance of any connection style class is an callable object,
26632674
whose call signature is::
@@ -3073,6 +3084,7 @@ def _point_along_a_line(x0, y0, x1, y1, d):
30733084
return x2, y2
30743085

30753086

3087+
@_docstring.dedent_interpd
30763088
class ArrowStyle(_Style):
30773089
"""
30783090
`ArrowStyle` is a container class which defines several
@@ -3093,7 +3105,7 @@ class ArrowStyle(_Style):
30933105
30943106
The following classes are defined
30953107
3096-
%(AvailableArrowstyles)s
3108+
%(ArrowStyle:table)s
30973109
30983110
An instance of any arrow style class is a callable object,
30993111
whose call signature is::
@@ -3823,17 +3835,6 @@ def transmute(self, path, mutation_size, linewidth):
38233835
return path, True
38243836

38253837

3826-
_docstring.interpd.update(
3827-
AvailableBoxstyles=BoxStyle.pprint_styles(),
3828-
ListBoxstyles=_simpleprint_styles(BoxStyle._style_list),
3829-
AvailableArrowstyles=ArrowStyle.pprint_styles(),
3830-
AvailableConnectorstyles=ConnectionStyle.pprint_styles(),
3831-
)
3832-
_docstring.dedent_interpd(BoxStyle)
3833-
_docstring.dedent_interpd(ArrowStyle)
3834-
_docstring.dedent_interpd(ConnectionStyle)
3835-
3836-
38373838
class FancyBboxPatch(Patch):
38383839
"""
38393840
A fancy box around a rectangle with lower left at *xy* = (*x*, *y*)
@@ -3878,7 +3879,7 @@ def __init__(self, xy, width, height,
38783879
38793880
The following box styles are available:
38803881
3881-
%(AvailableBoxstyles)s
3882+
%(BoxStyle:table)s
38823883
38833884
mutation_scale : float, default: 1
38843885
Scaling factor applied to the attributes of the box style
@@ -3924,9 +3925,8 @@ def __init__(self, xy, width, height,
39243925
@_docstring.dedent_interpd
39253926
def set_boxstyle(self, boxstyle=None, **kwargs):
39263927
"""
3927-
Set the box style.
3928+
Set the box style, possibly with further attributes.
39283929
3929-
Most box styles can be further configured using attributes.
39303930
Attributes from the previous box style are not reused.
39313931
39323932
Without argument (or with ``boxstyle=None``), the available box styles
@@ -3935,17 +3935,14 @@ def set_boxstyle(self, boxstyle=None, **kwargs):
39353935
Parameters
39363936
----------
39373937
boxstyle : str or `matplotlib.patches.BoxStyle`
3938-
The style of the fancy box. This can either be a `.BoxStyle`
3939-
instance or a string of the style name and optionally comma
3940-
separated attributes (e.g. "Round, pad=0.2"). This string is
3941-
passed to `.BoxStyle` to construct a `.BoxStyle` object. See
3942-
there for a full documentation.
3938+
The style of the box: either a `.BoxStyle` instance, or a string,
3939+
which is the style name and optionally comma separated attributes
3940+
(e.g. "Round,pad=0.2"). Such a string is used to construct a
3941+
`.BoxStyle` object, as documented in that class.
39433942
39443943
The following box styles are available:
39453944
3946-
%(AvailableBoxstyles)s
3947-
3948-
.. ACCEPTS: %(ListBoxstyles)s
3945+
%(BoxStyle:table_and_accepts)s
39493946
39503947
**kwargs
39513948
Additional attributes for the box style. See the table above for
@@ -3955,17 +3952,20 @@ def set_boxstyle(self, boxstyle=None, **kwargs):
39553952
--------
39563953
::
39573954
3958-
set_boxstyle("round,pad=0.2")
3955+
set_boxstyle("Round,pad=0.2")
39593956
set_boxstyle("round", pad=0.2)
3960-
39613957
"""
39623958
if boxstyle is None:
39633959
return BoxStyle.pprint_styles()
39643960
self._bbox_transmuter = (
3965-
BoxStyle(boxstyle, **kwargs) if isinstance(boxstyle, str)
3966-
else boxstyle)
3961+
BoxStyle(boxstyle, **kwargs)
3962+
if isinstance(boxstyle, str) else boxstyle)
39673963
self.stale = True
39683964

3965+
def get_boxstyle(self):
3966+
"""Return the boxstyle object."""
3967+
return self._bbox_transmuter
3968+
39693969
def set_mutation_scale(self, scale):
39703970
"""
39713971
Set the mutation scale.
@@ -3997,10 +3997,6 @@ def get_mutation_aspect(self):
39973997
return (self._mutation_aspect if self._mutation_aspect is not None
39983998
else 1) # backcompat.
39993999

4000-
def get_boxstyle(self):
4001-
"""Return the boxstyle object."""
4002-
return self._bbox_transmuter
4003-
40044000
def get_path(self):
40054001
"""Return the mutated path of the rectangle."""
40064002
boxstyle = self.get_boxstyle()
@@ -4176,7 +4172,7 @@ def __init__(self, posA=None, posB=None, path=None,
41764172
meant to be scaled with the *mutation_scale*. The following arrow
41774173
styles are available:
41784174
4179-
%(AvailableArrowstyles)s
4175+
%(ArrowStyle:table)s
41804176
41814177
connectionstyle : str or `.ConnectionStyle` or None, optional, \
41824178
default: 'arc3'
@@ -4185,7 +4181,7 @@ def __init__(self, posA=None, posB=None, path=None,
41854181
names, with optional comma-separated attributes. The following
41864182
connection styles are available:
41874183
4188-
%(AvailableConnectorstyles)s
4184+
%(ConnectionStyle:table)s
41894185
41904186
patchA, patchB : `.Patch`, default: None
41914187
Head and tail patches, respectively.
@@ -4282,34 +4278,44 @@ def set_patchB(self, patchB):
42824278
self.patchB = patchB
42834279
self.stale = True
42844280

4285-
def set_connectionstyle(self, connectionstyle, **kwargs):
4281+
@_docstring.dedent_interpd
4282+
def set_connectionstyle(self, connectionstyle=None, **kwargs):
42864283
"""
4287-
Set the connection style. Old attributes are forgotten.
4284+
Set the connection style, possibly with further attributes.
4285+
4286+
Attributes from the previous connection style are not reused.
4287+
4288+
Without argument (or with ``connectionstyle=None``), the available box
4289+
styles are returned as a human-readable string.
42884290
42894291
Parameters
42904292
----------
4291-
connectionstyle : str or `.ConnectionStyle` or None, optional
4292-
Can be a string with connectionstyle name with
4293-
optional comma-separated attributes, e.g.::
4293+
connectionstyle : str or `matplotlib.patches.ConnectionStyle`
4294+
The style of the connection: either a `.ConnectionStyle` instance,
4295+
or a string, which is the style name and optionally comma separated
4296+
attributes (e.g. "Arc,armA=30,rad=10"). Such a string is used to
4297+
construct a `.ConnectionStyle` object, as documented in that class.
42944298
4295-
set_connectionstyle("arc,angleA=0,armA=30,rad=10")
4299+
The following connection styles are available:
42964300
4297-
Alternatively, the attributes can be provided as keywords, e.g.::
4301+
%(ConnectionStyle:table_and_accepts)s
42984302
4299-
set_connectionstyle("arc", angleA=0,armA=30,rad=10)
4303+
**kwargs
4304+
Additional attributes for the connection style. See the table above
4305+
for supported parameters.
43004306
4301-
Without any arguments (or with ``connectionstyle=None``), return
4302-
available styles as a list of strings.
4303-
"""
4307+
Examples
4308+
--------
4309+
::
43044310
4311+
set_connectionstyle("Arc,armA=30,rad=10")
4312+
set_connectionstyle("arc", armA=30, rad=10)
4313+
"""
43054314
if connectionstyle is None:
43064315
return ConnectionStyle.pprint_styles()
4307-
4308-
if (isinstance(connectionstyle, ConnectionStyle._Base) or
4309-
callable(connectionstyle)):
4310-
self._connector = connectionstyle
4311-
else:
4312-
self._connector = ConnectionStyle(connectionstyle, **kwargs)
4316+
self._connector = (
4317+
ConnectionStyle(connectionstyle, **kwargs)
4318+
if isinstance(connectionstyle, str) else connectionstyle)
43134319
self.stale = True
43144320

43154321
def get_connectionstyle(self):
@@ -4318,31 +4324,41 @@ def get_connectionstyle(self):
43184324

43194325
def set_arrowstyle(self, arrowstyle=None, **kwargs):
43204326
"""
4321-
Set the arrow style. Old attributes are forgotten. Without arguments
4322-
(or with ``arrowstyle=None``) returns available box styles as a list of
4323-
strings.
4327+
Set the arrow style, possibly with further attributes.
4328+
4329+
Attributes from the previous arrow style are not reused.
4330+
4331+
Without argument (or with ``arrowstyle=None``), the available box
4332+
styles are returned as a human-readable string.
43244333
43254334
Parameters
43264335
----------
4327-
arrowstyle : None or ArrowStyle or str, default: None
4328-
Can be a string with arrowstyle name with optional comma-separated
4329-
attributes, e.g.::
4336+
arrowstyle : str or `matplotlib.patches.ArrowStyle`
4337+
The style of the arrow: either a `.ArrowStyle` instance, or a
4338+
string, which is the style name and optionally comma separated
4339+
attributes (e.g. "Fancy,head_length=0.2"). Such a string is used to
4340+
construct a `.ArrowStyle` object, as documented in that class.
43304341
4331-
set_arrowstyle("Fancy,head_length=0.2")
4342+
The following arrow styles are available:
43324343
4333-
Alternatively attributes can be provided as keywords, e.g.::
4344+
%(ArrowStyle:table_and_accepts)s
43344345
4335-
set_arrowstyle("fancy", head_length=0.2)
4346+
**kwargs
4347+
Additional attributes for the arrow style. See the table above for
4348+
supported parameters.
43364349
4337-
"""
4350+
Examples
4351+
--------
4352+
::
43384353
4354+
set_arrowstyle("Fancy,head_length=0.2")
4355+
set_arrowstyle("fancy", head_length=0.2)
4356+
"""
43394357
if arrowstyle is None:
43404358
return ArrowStyle.pprint_styles()
4341-
4342-
if isinstance(arrowstyle, ArrowStyle._Base):
4343-
self._arrow_transmuter = arrowstyle
4344-
else:
4345-
self._arrow_transmuter = ArrowStyle(arrowstyle, **kwargs)
4359+
self._arrow_transmuter = (
4360+
ArrowStyle(arrowstyle, **kwargs)
4361+
if isinstance(arrowstyle, str) else arrowstyle)
43464362
self.stale = True
43474363

43484364
def get_arrowstyle(self):

0 commit comments

Comments
 (0)