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,40 @@ 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 the axis with the
95
+ specified units.
96
+ """
82
97
return None
83
98
84
99
@staticmethod
85
100
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
+ """
87
104
return None
88
105
89
106
@staticmethod
90
107
def convert (obj , unit , axis ):
91
108
"""
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.
95
113
"""
96
114
return obj
97
115
98
116
@staticmethod
99
117
def is_numlike (x ):
100
118
"""
101
- The matplotlib datalim, autoscaling, locators etc work with
119
+ The Matplotlib datalim, autoscaling, locators etc work with
102
120
scalars which are the units converted to floats given the
103
121
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.
108
123
"""
109
124
if iterable (x ):
110
125
for thisx in x :
@@ -115,14 +130,16 @@ def is_numlike(x):
115
130
116
131
class Registry (dict ):
117
132
"""
118
- register types with conversion interface
133
+ Register types with conversion interface.
119
134
"""
120
135
def __init__ (self ):
121
136
dict .__init__ (self )
122
137
self ._cached = {}
123
138
124
139
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
+ """
126
143
127
144
if not len (self ):
128
145
return None # nothing registered
@@ -153,7 +170,7 @@ def get_converter(self, x):
153
170
# not ever return a non-subclass for a single element.
154
171
next_item = xravel [0 ]
155
172
if (not isinstance (next_item , np .ndarray ) or
156
- next_item .shape != x .shape ):
173
+ next_item .shape != x .shape ):
157
174
converter = self .get_converter (next_item )
158
175
return converter
159
176
0 commit comments