Skip to content

Commit 86d670f

Browse files
committed
cbook docs cleanup
1 parent fac4ad5 commit 86d670f

File tree

1 file changed

+97
-67
lines changed

1 file changed

+97
-67
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 97 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,11 @@ def process(self, s, *args, **kwargs):
200200

201201
class silent_list(list):
202202
"""
203-
override repr when returning a list of matplotlib artists to
204-
prevent long, meaningless output. This is meant to be used for a
205-
homogeneous list of a given type
203+
A list with a short ``repr()``.
204+
205+
This is meant to be used for a homogeneous list of a given type. In
206+
particular, this is intended to be used for returning a list of artists
207+
to prevent long, meaningless output.
206208
"""
207209
def __init__(self, type, seq=None):
208210
self.type = type
@@ -226,7 +228,7 @@ def __setstate__(self, state):
226228
class IgnoredKeywordWarning(UserWarning):
227229
"""
228230
A class for issuing warnings about keyword arguments that will be ignored
229-
by matplotlib
231+
by Matplotlib.
230232
"""
231233
pass
232234

@@ -235,7 +237,7 @@ def local_over_kwdict(local_var, kwargs, *keys):
235237
"""
236238
Enforces the priority of a local variable over potentially conflicting
237239
argument(s) from a kwargs dict. The following possible output values are
238-
considered in order of priority:
240+
considered in order of priority::
239241
240242
local_var > kwargs[keys[0]] > ... > kwargs[keys[-1]]
241243
@@ -245,26 +247,26 @@ def local_over_kwdict(local_var, kwargs, *keys):
245247
246248
Parameters
247249
----------
248-
local_var : any object
249-
The local variable (highest priority)
250+
local_var : any object
251+
The local variable (highest priority).
250252
251-
kwargs : dict
252-
Dictionary of keyword arguments; modified in place
253+
kwargs : dict
254+
Dictionary of keyword arguments; modified in place.
253255
254-
keys : str(s)
255-
Name(s) of keyword arguments to process, in descending order of
256-
priority
256+
keys : str(s)
257+
Name(s) of keyword arguments to process, in descending order of
258+
priority.
257259
258260
Returns
259261
-------
260-
out : any object
261-
Either local_var or one of kwargs[key] for key in keys
262+
out : any object
263+
Either local_var or one of kwargs[key] for key in keys.
262264
263265
Raises
264266
------
265-
IgnoredKeywordWarning
266-
For each key in keys that is removed from kwargs but not used as
267-
the output value
267+
IgnoredKeywordWarning
268+
For each key in keys that is removed from kwargs but not used as
269+
the output value.
268270
269271
"""
270272
out = local_var
@@ -447,7 +449,7 @@ def _get_data_path(*args):
447449

448450
def flatten(seq, scalarp=is_scalar_or_string):
449451
"""
450-
Return a generator of flattened nested containers
452+
Return a generator of flattened nested containers.
451453
452454
For example:
453455
@@ -526,9 +528,12 @@ def dedent(s):
526528

527529
class maxdict(dict):
528530
"""
529-
A dictionary with a maximum size; this doesn't override all the
530-
relevant methods to constrain the size, just setitem, so use with
531-
caution
531+
A dictionary with a maximum size.
532+
533+
Notes
534+
-----
535+
This doesn't override all the relevant methods to constrain the size,
536+
just ``__setitem__``, so use with caution.
532537
"""
533538
def __init__(self, maxsize):
534539
dict.__init__(self)
@@ -702,15 +707,18 @@ def safe_masked_invalid(x, copy=False):
702707

703708
def print_cycles(objects, outstream=sys.stdout, show_progress=False):
704709
"""
705-
*objects*
706-
A list of objects to find cycles in. It is often useful to
707-
pass in gc.garbage to find the cycles that are preventing some
708-
objects from being garbage collected.
710+
Print loops of cyclic references in the given *objects*.
709711
710-
*outstream*
711-
The stream for output.
712+
It is often useful to pass in ``gc.garbage`` to find the cycles that are
713+
preventing some objects from being garbage collected.
712714
713-
*show_progress*
715+
Parameters
716+
----------
717+
objects
718+
A list of objects to find cycles in.
719+
outstream
720+
The stream for output.
721+
show_progress : bool
714722
If True, print the number of objects reached as they are found.
715723
"""
716724
import gc
@@ -868,17 +876,18 @@ def simple_linear_interpolation(a, steps):
868876
"""
869877
Resample an array with ``steps - 1`` points between original point pairs.
870878
879+
Along each column of *a*, ``(steps - 1)`` points are introduced between
880+
each original values; the values are linearly interpolated.
881+
871882
Parameters
872883
----------
873884
a : array, shape (n, ...)
874885
steps : int
875886
876887
Returns
877888
-------
878-
array, shape ``((n - 1) * steps + 1, ...)``
879-
880-
Along each column of *a*, ``(steps - 1)`` points are introduced between
881-
each original values; the values are linearly interpolated.
889+
array
890+
shape ``((n - 1) * steps + 1, ...)``
882891
"""
883892
fps = a.reshape((len(a), -1))
884893
xp = np.arange(len(a)) * steps
@@ -1246,8 +1255,8 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
12461255

12471256
def contiguous_regions(mask):
12481257
"""
1249-
Return a list of (ind0, ind1) such that mask[ind0:ind1].all() is
1250-
True and we cover all such regions
1258+
Return a list of (ind0, ind1) such that ``mask[ind0:ind1].all()`` is
1259+
True and we cover all such regions.
12511260
"""
12521261
mask = np.asarray(mask, dtype=bool)
12531262

@@ -1271,8 +1280,12 @@ def contiguous_regions(mask):
12711280

12721281

12731282
def is_math_text(s):
1274-
# Did we find an even number of non-escaped dollar signs?
1275-
# If so, treat is as math text.
1283+
"""
1284+
Returns whether the string *s* contains math expressions.
1285+
1286+
This is done by checking whether *s* contains an even number of
1287+
non-escaped dollar signs.
1288+
"""
12761289
s = str(s)
12771290
dollar_count = s.count(r'$') - s.count(r'\$')
12781291
even_dollars = (dollar_count > 0 and dollar_count % 2 == 0)
@@ -1333,10 +1346,14 @@ def _reshape_2D(X, name):
13331346
def violin_stats(X, method, points=100, quantiles=None):
13341347
"""
13351348
Returns a list of dictionaries of data which can be used to draw a series
1336-
of violin plots. See the `Returns` section below to view the required keys
1337-
of the dictionary. Users can skip this function and pass a user-defined set
1338-
of dictionaries to the `axes.vplot` method instead of using Matplotlib to
1339-
do the calculations.
1349+
of violin plots.
1350+
1351+
See the Returns section below to view the required keys of the dictionary.
1352+
1353+
Users can skip this function and pass a user-defined set of dictionaries
1354+
with the same keys to `~.axes.Axes.violinplot` instead of using Matplotlib
1355+
to do the calculations. See the *Returns* section below for the keys
1356+
that must be present in the dictionaries.
13401357
13411358
Parameters
13421359
----------
@@ -1350,7 +1367,7 @@ def violin_stats(X, method, points=100, quantiles=None):
13501367
return a vector of the values of the KDE evaluated at the values
13511368
specified in coords.
13521369
1353-
points : scalar, default = 100
1370+
points : int, default = 100
13541371
Defines the number of points to evaluate each of the gaussian kernel
13551372
density estimates at.
13561373
@@ -1362,8 +1379,9 @@ def violin_stats(X, method, points=100, quantiles=None):
13621379
13631380
Returns
13641381
-------
1365-
A list of dictionaries containing the results for each column of data.
1366-
The dictionaries contain at least the following:
1382+
vpstats : list of dict
1383+
A list of dictionaries containing the results for each column of data.
1384+
The dictionaries contain at least the following:
13671385
13681386
- coords: A list of scalars containing the coordinates this particular
13691387
kernel density estimate was evaluated at.
@@ -1448,7 +1466,7 @@ def pts_to_prestep(x, *args):
14481466
14491467
Examples
14501468
--------
1451-
>> x_s, y1_s, y2_s = pts_to_prestep(x, y1, y2)
1469+
>>> x_s, y1_s, y2_s = pts_to_prestep(x, y1, y2)
14521470
"""
14531471
steps = np.zeros((1 + len(args), max(2 * len(x) - 1, 0)))
14541472
# In all `pts_to_*step` functions, only assign *once* using `x` and `args`,
@@ -1486,7 +1504,7 @@ def pts_to_poststep(x, *args):
14861504
14871505
Examples
14881506
--------
1489-
>> x_s, y1_s, y2_s = pts_to_poststep(x, y1, y2)
1507+
>>> x_s, y1_s, y2_s = pts_to_poststep(x, y1, y2)
14901508
"""
14911509
steps = np.zeros((1 + len(args), max(2 * len(x) - 1, 0)))
14921510
steps[0, 0::2] = x
@@ -1522,7 +1540,7 @@ def pts_to_midstep(x, *args):
15221540
15231541
Examples
15241542
--------
1525-
>> x_s, y1_s, y2_s = pts_to_midstep(x, y1, y2)
1543+
>>> x_s, y1_s, y2_s = pts_to_midstep(x, y1, y2)
15261544
"""
15271545
steps = np.zeros((1 + len(args), 2 * len(x)))
15281546
x = np.asanyarray(x)
@@ -1543,19 +1561,19 @@ def pts_to_midstep(x, *args):
15431561

15441562
def index_of(y):
15451563
"""
1546-
A helper function to get the index of an input to plot
1547-
against if x values are not explicitly given.
1564+
A helper function to create reasonable x values for the given *y*.
1565+
1566+
This is used for plotting (x, y) if x values are not explicitly given.
15481567
1549-
Tries to get `y.index` (works if this is a pd.Series), if that
1550-
fails, return np.arange(y.shape[0]).
1568+
First try ``y.index`` (assuming *y* is a `pandas.Series`), if that
1569+
fails, use ``range(len(y))``.
15511570
15521571
This will be extended in the future to deal with more types of
15531572
labeled data.
15541573
15551574
Parameters
15561575
----------
15571576
y : scalar or array-like
1558-
The proposed y-value
15591577
15601578
Returns
15611579
-------
@@ -1570,6 +1588,12 @@ def index_of(y):
15701588

15711589

15721590
def safe_first_element(obj):
1591+
"""
1592+
Return the first element in *obj*.
1593+
1594+
This is an type-independent way of obtaining the first element, supporting
1595+
both index access and the iterator protocol.
1596+
"""
15731597
if isinstance(obj, collections.abc.Iterator):
15741598
# needed to accept `array.flat` as input.
15751599
# np.flatiter reports as an instance of collections.Iterator
@@ -1586,27 +1610,33 @@ def safe_first_element(obj):
15861610

15871611

15881612
def sanitize_sequence(data):
1589-
"""Converts dictview object to list"""
1613+
"""
1614+
Convert dictview objects to list. Other inputs are returned unchanged.
1615+
"""
15901616
return (list(data) if isinstance(data, collections.abc.MappingView)
15911617
else data)
15921618

15931619

15941620
def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
15951621
allowed=None):
1596-
"""Helper function to normalize kwarg inputs
1622+
"""
1623+
Helper function to normalize kwarg inputs.
15971624
15981625
The order they are resolved are:
15991626
1600-
1. aliasing
1601-
2. required
1602-
3. forbidden
1603-
4. allowed
1627+
1. aliasing
1628+
2. required
1629+
3. forbidden
1630+
4. allowed
16041631
16051632
This order means that only the canonical names need appear in
1606-
`allowed`, `forbidden`, `required`
1633+
*allowed*, *forbidden*, *required*.
16071634
16081635
Parameters
16091636
----------
1637+
kw : dict
1638+
A dict of keyword arguments.
1639+
16101640
alias_mapping : dict or Artist subclass or Artist instance, optional
16111641
A mapping between a canonical name to a list of
16121642
aliases, in order of precedence from lowest to highest.
@@ -1617,17 +1647,17 @@ def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
16171647
If an Artist subclass or instance is passed, use its properties alias
16181648
mapping.
16191649
1620-
required : iterable, optional
1621-
A tuple of fields that must be in kwargs.
1650+
required : list of str, optional
1651+
A list of keys that must be in *kws*.
16221652
1623-
forbidden : iterable, optional
1624-
A list of keys which may not be in kwargs
1653+
forbidden : list of str, optional
1654+
A list of keys which may not be in *kw*.
16251655
1626-
allowed : tuple, optional
1627-
A tuple of allowed fields. If this not None, then raise if
1628-
`kw` contains any keys not in the union of `required`
1629-
and `allowed`. To allow only the required fields pass in
1630-
``()`` for `allowed`
1656+
allowed : list of str, optional
1657+
A list of allowed fields. If this not None, then raise if
1658+
*kw* contains any keys not in the union of *required*
1659+
and *allowed*. To allow only the required fields pass in
1660+
an empty tuple ``allowed=()``.
16311661
16321662
Raises
16331663
------

0 commit comments

Comments
 (0)