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

Skip to content

Commit a3d1c46

Browse files
authored
Merge pull request #10370 from dstansby/units-clean
Clean up units.py
2 parents 2e83a8a + 5464f3f commit a3d1c46

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

lib/matplotlib/units.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
22
The classes here provide support for using custom classes with
3-
matplotlib, e.g., those that do not expose the array interface but know
3+
Matplotlib, e.g., those that do not expose the array interface but know
44
how to convert themselves to arrays. It also supports classes with
55
units and units conversion. Use cases include converters for custom
66
objects, e.g., a list of datetime objects, as well as for objects that
77
are unit aware. We don't assume any particular units implementation;
88
rather a units implementation must provide the register with the Registry
9-
converter dictionary and a ConversionInterface. For example,
9+
converter dictionary and a `ConversionInterface`. For example,
1010
here is a complete implementation which supports plotting with native
1111
datetime objects::
1212
@@ -37,7 +37,7 @@ def default_units(x, axis):
3737
'return the default unit for x or None'
3838
return 'date'
3939
40-
# finally we register our object type with a converter
40+
# Finally we register our object type with a converter
4141
units.registry[datetime.date] = DateConverter()
4242
4343
"""
@@ -51,17 +51,29 @@ def default_units(x, axis):
5151

5252

5353
class AxisInfo(object):
54-
"""information to support default axis labeling and tick labeling, and
55-
default limits"""
54+
"""
55+
Information to support default axis labeling, tick labeling, and
56+
default limits.
57+
"""
5658
def __init__(self, majloc=None, minloc=None,
5759
majfmt=None, minfmt=None, label=None,
5860
default_limits=None):
5961
"""
60-
majloc and minloc: TickLocators for the major and minor ticks
61-
majfmt and minfmt: TickFormatters for the major and minor ticks
62-
label: the default axis label
63-
default_limits: the default min, max of the axis if no data is present
64-
If any of the above are None, the axis will simply use the default
62+
Parameters
63+
----------
64+
majloc, minloc
65+
TickLocators for the major and minor ticks.
66+
majfmt, minfmt
67+
TickFormatters for the major and minor ticks.
68+
label
69+
The default axis label.
70+
default_limits
71+
The default min, max of the axis if no data is present.
72+
73+
Notes
74+
-----
75+
If any of the above are ``None``, the axis will simply use the
76+
default value.
6577
"""
6678
self.majloc = majloc
6779
self.minloc = minloc
@@ -74,37 +86,40 @@ def __init__(self, majloc=None, minloc=None,
7486
class ConversionInterface(object):
7587
"""
7688
The minimal interface for a converter to take custom instances (or
77-
sequences) and convert them to values mpl can use
89+
sequences) and convert them to values Matplotlib can use.
7890
"""
7991
@staticmethod
8092
def axisinfo(unit, axis):
81-
'return an units.AxisInfo instance for axis with the specified units'
93+
"""
94+
Return an `~units.AxisInfo` instance for the axis with the
95+
specified units.
96+
"""
8297
return None
8398

8499
@staticmethod
85100
def default_units(x, axis):
86-
'return the default unit for x or None for the given axis'
101+
"""
102+
Return the default unit for *x* or ``None`` for the given axis.
103+
"""
87104
return None
88105

89106
@staticmethod
90107
def convert(obj, unit, axis):
91108
"""
92-
convert obj using unit for the specified axis. If obj is a sequence,
93-
return the converted sequence. The output must be a sequence of
94-
scalars that can be used by the numpy array layer
109+
Convert *obj* using *unit* for the specified *axis*.
110+
If *obj* is a sequence, return the converted sequence.
111+
The output must be a sequence of scalars that can be used by the numpy
112+
array layer.
95113
"""
96114
return obj
97115

98116
@staticmethod
99117
def is_numlike(x):
100118
"""
101-
The matplotlib datalim, autoscaling, locators etc work with
119+
The Matplotlib datalim, autoscaling, locators etc work with
102120
scalars which are the units converted to floats given the
103121
current unit. The converter may be passed these floats, or
104-
arrays of them, even when units are set. Derived conversion
105-
interfaces may opt to pass plain-ol unitless numbers through
106-
the conversion interface and this is a helper function for
107-
them.
122+
arrays of them, even when units are set.
108123
"""
109124
if iterable(x):
110125
for thisx in x:
@@ -115,14 +130,16 @@ def is_numlike(x):
115130

116131
class Registry(dict):
117132
"""
118-
register types with conversion interface
133+
Register types with conversion interface.
119134
"""
120135
def __init__(self):
121136
dict.__init__(self)
122137
self._cached = {}
123138

124139
def get_converter(self, x):
125-
'get the converter interface instance for x, or None'
140+
"""
141+
Get the converter interface instance for *x*, or ``None``.
142+
"""
126143

127144
if not len(self):
128145
return None # nothing registered
@@ -153,7 +170,7 @@ def get_converter(self, x):
153170
# not ever return a non-subclass for a single element.
154171
next_item = xravel[0]
155172
if (not isinstance(next_item, np.ndarray) or
156-
next_item.shape != x.shape):
173+
next_item.shape != x.shape):
157174
converter = self.get_converter(next_item)
158175
return converter
159176

0 commit comments

Comments
 (0)