1
1
"""
2
2
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
4
4
how to convert themselves to arrays. It also supports classes with
5
5
units and units conversion. Use cases include converters for custom
6
6
objects, e.g., a list of datetime objects, as well as for objects that
7
7
are unit aware. We don't assume any particular units implementation;
8
8
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,
10
10
here is a complete implementation which supports plotting with native
11
11
datetime objects::
12
12
@@ -37,7 +37,7 @@ def default_units(x, axis):
37
37
'return the default unit for x or None'
38
38
return 'date'
39
39
40
- # finally we register our object type with a converter
40
+ # Finally we register our object type with a converter
41
41
units.registry[datetime.date] = DateConverter()
42
42
43
43
"""
@@ -51,17 +51,29 @@ def default_units(x, axis):
51
51
52
52
53
53
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
+ """
56
58
def __init__ (self , majloc = None , minloc = None ,
57
59
majfmt = None , minfmt = None , label = None ,
58
60
default_limits = None ):
59
61
"""
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.
65
77
"""
66
78
self .majloc = majloc
67
79
self .minloc = minloc
@@ -74,37 +86,37 @@ def __init__(self, majloc=None, minloc=None,
74
86
class ConversionInterface (object ):
75
87
"""
76
88
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.
78
90
"""
79
91
@staticmethod
80
92
def axisinfo (unit , axis ):
81
- 'return an units.AxisInfo instance for axis with the specified units'
93
+ '''
94
+ Return an `~units.AxisInfo` instance for axis with the specified units.
95
+ '''
82
96
return None
83
97
84
98
@staticmethod
85
99
def default_units (x , axis ):
86
- 'return the default unit for x or None for the given axis'
100
+ 'Return the default unit for *x* or `` None`` for the given axis. '
87
101
return None
88
102
89
103
@staticmethod
90
104
def convert (obj , unit , axis ):
91
105
"""
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
106
+ Convert *obj* using *unit* for the specified *axis*.
107
+ If *obj* is a sequence, return the converted sequence.
108
+ The output must be a sequence of scalars that can be used by the numpy
109
+ array layer.
95
110
"""
96
111
return obj
97
112
98
113
@staticmethod
99
114
def is_numlike (x ):
100
115
"""
101
- The matplotlib datalim, autoscaling, locators etc work with
116
+ The Matplotlib datalim, autoscaling, locators etc work with
102
117
scalars which are the units converted to floats given the
103
118
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.
119
+ arrays of them, even when units are set.
108
120
"""
109
121
if iterable (x ):
110
122
for thisx in x :
@@ -115,14 +127,14 @@ def is_numlike(x):
115
127
116
128
class Registry (dict ):
117
129
"""
118
- register types with conversion interface
130
+ Register types with conversion interface.
119
131
"""
120
132
def __init__ (self ):
121
133
dict .__init__ (self )
122
134
self ._cached = {}
123
135
124
136
def get_converter (self , x ):
125
- 'get the converter interface instance for x , or None'
137
+ 'Get the converter interface instance for *x* , or `` None``. '
126
138
127
139
if not len (self ):
128
140
return None # nothing registered
@@ -153,7 +165,7 @@ def get_converter(self, x):
153
165
# not ever return a non-subclass for a single element.
154
166
next_item = xravel [0 ]
155
167
if (not isinstance (next_item , np .ndarray ) or
156
- next_item .shape != x .shape ):
168
+ next_item .shape != x .shape ):
157
169
converter = self .get_converter (next_item )
158
170
return converter
159
171
0 commit comments