Skip to content

Commit 795023a

Browse files
committed
Cleanup of scales docs
1 parent f9e71a5 commit 795023a

File tree

2 files changed

+87
-79
lines changed

2 files changed

+87
-79
lines changed

lib/matplotlib/scale.py

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""
2+
Scales define the distribution of data values on an axis, e.g. a log scaling.
3+
4+
They are attached to an `~.axis.Axis` and hold a `.Transform`, which is
5+
responsible for the actual data transformation.
6+
7+
See also `.axes.Axes.set_xscale` and the scales examples in the documentation.
8+
"""
9+
110
import inspect
211
import textwrap
312

@@ -20,12 +29,14 @@ class ScaleBase:
2029
2130
Any subclasses will want to override:
2231
23-
- :attr:`name`
24-
- :meth:`get_transform`
25-
- :meth:`set_default_locators_and_formatters`
32+
- :attr:`name`
33+
- :meth:`get_transform`
34+
- :meth:`set_default_locators_and_formatters`
2635
2736
And optionally:
28-
- :meth:`limit_range_for_scale`
37+
38+
- :meth:`limit_range_for_scale`
39+
2940
"""
3041

3142
def __init__(self, axis, **kwargs):
@@ -51,9 +62,8 @@ def get_transform(self):
5162

5263
def set_default_locators_and_formatters(self, axis):
5364
"""
54-
Set the :class:`~matplotlib.ticker.Locator` and
55-
:class:`~matplotlib.ticker.Formatter` objects on the given
56-
axis to match this scale.
65+
Set the locators and formatters of *axis* to instances suitable for
66+
this scale.
5767
"""
5868
raise NotImplementedError()
5969

@@ -63,7 +73,7 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
6373
domain supported by this scale.
6474
6575
*minpos* should be the minimum positive value in the data.
66-
This is used by log scales to determine a minimum value.
76+
This is used by log scales to determine a minimum value.
6777
"""
6878
return vmin, vmax
6979

@@ -84,10 +94,7 @@ def __init__(self, axis, **kwargs):
8494
super().__init__(axis, **kwargs)
8595

8696
def set_default_locators_and_formatters(self, axis):
87-
"""
88-
Set the locators and formatters to reasonable defaults for
89-
linear scaling.
90-
"""
97+
# docstring inherited
9198
axis.set_major_locator(AutoLocator())
9299
axis.set_major_formatter(ScalarFormatter())
93100
axis.set_minor_formatter(NullFormatter())
@@ -100,8 +107,8 @@ def set_default_locators_and_formatters(self, axis):
100107

101108
def get_transform(self):
102109
"""
103-
The transform for linear scaling is just the
104-
:class:`~matplotlib.transforms.IdentityTransform`.
110+
Return the transform for linear scaling, which is just the
111+
`~matplotlib.transforms.IdentityTransform`.
105112
"""
106113
return IdentityTransform()
107114

@@ -157,8 +164,8 @@ def __init__(self, axis, functions):
157164
"""
158165
Parameters
159166
----------
160-
axis: the axis for the scale
161-
167+
axis : `matplotlib.axis.Axis`
168+
The axis for the scale.
162169
functions : (callable, callable)
163170
two-tuple of the forward and inverse functions for the scale.
164171
The forward function must be monotonic.
@@ -172,16 +179,12 @@ def forward(values: array-like) -> array-like
172179
self._transform = transform
173180

174181
def get_transform(self):
175-
"""
176-
The transform for arbitrary scaling
177-
"""
182+
"""Return the `.FuncTransform` associated with this scale."""
183+
# docstring inherited
178184
return self._transform
179185

180186
def set_default_locators_and_formatters(self, axis):
181-
"""
182-
Set the locators and formatters to the same defaults as the
183-
linear scale.
184-
"""
187+
# docstring inherited
185188
axis.set_major_locator(AutoLocator())
186189
axis.set_major_formatter(ScalarFormatter())
187190
axis.set_minor_formatter(NullFormatter())
@@ -354,18 +357,17 @@ class LogScale(ScaleBase):
354357

355358
def __init__(self, axis, **kwargs):
356359
"""
357-
*basex*/*basey*:
360+
Parameters
361+
----------
362+
basex, basey : float
358363
The base of the logarithm
359-
360-
*nonposx*/*nonposy*: {'mask', 'clip'}
361-
non-positive values in *x* or *y* can be masked as
362-
invalid, or clipped to a very small positive number
363-
364-
*subsx*/*subsy*:
364+
nonposx, nonposy : {'mask', 'clip'}
365+
Determines the behavior for non-positive values. They can either
366+
be masked as invalid, or clipped to a number very small positive
367+
number.
368+
subsx, subsy : sequence of int
365369
Where to place the subticks between each major tick.
366-
Should be a sequence of integers. For example, in a log10
367-
scale: ``[2, 3, 4, 5, 6, 7, 8, 9]``
368-
370+
For example, in a log10 scale: ``[2, 3, 4, 5, 6, 7, 8, 9]``
369371
will place 8 logarithmically spaced minor ticks between
370372
each major tick.
371373
"""
@@ -397,10 +399,7 @@ def base(self):
397399
return self._transform.base
398400

399401
def set_default_locators_and_formatters(self, axis):
400-
"""
401-
Set the locators and formatters to specialized versions for
402-
log scaling.
403-
"""
402+
# docstring inherited
404403
axis.set_major_locator(LogLocator(self.base))
405404
axis.set_major_formatter(LogFormatterSciNotation(self.base))
406405
axis.set_minor_locator(LogLocator(self.base, self.subs))
@@ -409,10 +408,7 @@ def set_default_locators_and_formatters(self, axis):
409408
labelOnlyBase=(self.subs is not None)))
410409

411410
def get_transform(self):
412-
"""
413-
Return a :class:`~matplotlib.transforms.Transform` instance
414-
appropriate for the given logarithm base.
415-
"""
411+
"""Return the `.LogTransform` associated with this scale."""
416412
return self._transform
417413

418414
def limit_range_for_scale(self, vmin, vmax, minpos):
@@ -438,8 +434,8 @@ def __init__(self, axis, functions, base=10):
438434
"""
439435
Parameters
440436
----------
441-
axis: the axis for the scale
442-
437+
axis : `matplotlib.axis.Axis`
438+
The axis for the scale.
443439
functions : (callable, callable)
444440
two-tuple of the forward and inverse functions for the scale.
445441
The forward function must be monotonic.
@@ -461,9 +457,7 @@ def base(self):
461457
return self._transform._b.base # Base of the LogTransform.
462458

463459
def get_transform(self):
464-
"""
465-
The transform for arbitrary scaling
466-
"""
460+
"""Return the `.Transform` associated with this scale."""
467461
return self._transform
468462

469463

@@ -592,20 +586,15 @@ def __init__(self, axis, **kwargs):
592586
self.subs = subs
593587

594588
def set_default_locators_and_formatters(self, axis):
595-
"""
596-
Set the locators and formatters to specialized versions for
597-
symmetrical log scaling.
598-
"""
589+
# docstring inherited
599590
axis.set_major_locator(SymmetricalLogLocator(self.get_transform()))
600591
axis.set_major_formatter(LogFormatterSciNotation(self.base))
601592
axis.set_minor_locator(SymmetricalLogLocator(self.get_transform(),
602593
self.subs))
603594
axis.set_minor_formatter(NullFormatter())
604595

605596
def get_transform(self):
606-
"""
607-
Return a :class:`SymmetricalLogTransform` instance.
608-
"""
597+
"""Return the `.SymmetricalLogTransform` associated with this scale."""
609598
return self._transform
610599

611600

@@ -669,19 +658,22 @@ class LogitScale(ScaleBase):
669658

670659
def __init__(self, axis, nonpos='mask'):
671660
"""
672-
*nonpos*: {'mask', 'clip'}
673-
values beyond ]0, 1[ can be masked as invalid, or clipped to a number
674-
very close to 0 or 1
661+
Parameters
662+
----------
663+
axis : `matplotlib.axis.Axis`
664+
Currently used.
665+
nonpos : {'mask', 'clip'}
666+
Determines the behavior for values beyond ]0, 1[. They can either
667+
be masked as invalid, or clipped to a number very close to 0 or 1.
675668
"""
676669
self._transform = LogitTransform(nonpos)
677670

678671
def get_transform(self):
679-
"""
680-
Return a :class:`LogitTransform` instance.
681-
"""
672+
"""Return the `.LogitTransform` associated with this scale."""
682673
return self._transform
683674

684675
def set_default_locators_and_formatters(self, axis):
676+
# docstring inherited
685677
# ..., 0.01, 0.1, 0.5, 0.9, 0.99, ...
686678
axis.set_major_locator(LogitLocator())
687679
axis.set_major_formatter(LogitFormatter())
@@ -709,6 +701,7 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
709701

710702

711703
def get_scale_names():
704+
"""Return the names of the available scales."""
712705
return sorted(_scale_mapping)
713706

714707

@@ -719,22 +712,25 @@ def scale_factory(scale, axis, **kwargs):
719712
Parameters
720713
----------
721714
scale : {%(names)s}
722-
axis : Axis
715+
axis : `matplotlib.axis.Axis`
723716
"""
724717
scale = scale.lower()
725718
cbook._check_in_list(_scale_mapping, scale=scale)
726719
return _scale_mapping[scale](axis, **kwargs)
727720

728721
if scale_factory.__doc__:
729722
scale_factory.__doc__ = scale_factory.__doc__ % {
730-
"names": ", ".join(get_scale_names())}
723+
"names": ", ".join(f"'{s}'" for s in get_scale_names())}
731724

732725

733726
def register_scale(scale_class):
734727
"""
735728
Register a new kind of scale.
736729
737-
*scale_class* must be a subclass of :class:`ScaleBase`.
730+
Parameters
731+
----------
732+
scale_class : subclass of `ScaleBase`
733+
The scale to register.
738734
"""
739735
_scale_mapping[scale_class.name] = scale_class
740736

lib/matplotlib/transforms.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,18 +1188,18 @@ class Transform(TransformNode):
11881188
Subclasses of this class should override the following members (at
11891189
minimum):
11901190
1191-
- :attr:`input_dims`
1192-
- :attr:`output_dims`
1193-
- :meth:`transform`
1194-
- :attr:`is_separable`
1195-
- :attr:`has_inverse`
1196-
- :meth:`inverted` (if :attr:`has_inverse` is True)
1191+
- :attr:`input_dims`
1192+
- :attr:`output_dims`
1193+
- :meth:`transform`
1194+
- :attr:`is_separable`
1195+
- :attr:`has_inverse`
1196+
- :meth:`inverted` (if :attr:`has_inverse` is True)
11971197
11981198
If the transform needs to do something non-standard with
11991199
:class:`matplotlib.path.Path` objects, such as adding curves
12001200
where there were once line segments, it should override:
12011201
1202-
- :meth:`transform_path`
1202+
- :meth:`transform_path`
12031203
"""
12041204
input_dims = None
12051205
"""
@@ -1403,11 +1403,17 @@ def transform_affine(self, values):
14031403
affine transformations, this is equivalent to
14041404
``transform(values)``.
14051405
1406-
Accepts a numpy array of shape (N x :attr:`input_dims`) and
1407-
returns a numpy array of shape (N x :attr:`output_dims`).
1406+
Parameters
1407+
----------
1408+
values : array
1409+
The input values as numpy array of length :attr:`input_dims` or
1410+
shape (N x :attr:`input_dims`).
14081411
1409-
Alternatively, accepts a numpy array of length :attr:`input_dims`
1410-
and returns a numpy array of length :attr:`output_dims`.
1412+
Returns
1413+
-------
1414+
values : array
1415+
The output values as numpy array of length :attr:`input_dims` or
1416+
shape (N x :attr:`output_dims`), depending on the input.
14111417
"""
14121418
return self.get_affine().transform(values)
14131419

@@ -1422,11 +1428,17 @@ def transform_non_affine(self, values):
14221428
``transform(values)``. In affine transformations, this is
14231429
always a no-op.
14241430
1425-
Accepts a numpy array of shape (N x :attr:`input_dims`) and
1426-
returns a numpy array of shape (N x :attr:`output_dims`).
1431+
Parameters
1432+
----------
1433+
values : array
1434+
The input values as numpy array of length :attr:`input_dims` or
1435+
shape (N x :attr:`input_dims`).
14271436
1428-
Alternatively, accepts a numpy array of length :attr:`input_dims`
1429-
and returns a numpy array of length :attr:`output_dims`.
1437+
Returns
1438+
-------
1439+
values : array
1440+
The output values as numpy array of length :attr:`input_dims` or
1441+
shape (N x :attr:`output_dims`), depending on the input.
14301442
"""
14311443
return values
14321444

@@ -1555,11 +1567,11 @@ def inverted(self):
15551567
"""
15561568
Return the corresponding inverse transformation.
15571569
1570+
It holds ``x == self.inverted().transform(self.transform(x))``.
1571+
15581572
The return value of this method should be treated as
15591573
temporary. An update to *self* does not cause a corresponding
15601574
update to its inverted copy.
1561-
1562-
``x === self.inverted().transform(self.transform(x))``
15631575
"""
15641576
raise NotImplementedError()
15651577

0 commit comments

Comments
 (0)