1
1
"""
2
- Module that allows plotting of string "category" data. i.e.
3
- ``plot(['d', 'f', 'a'],[1, 2, 3])`` will plot three points with x-axis
4
- values of 'd', 'f', 'a'.
2
+ Plotting of string "category" data: ``plot(['d', 'f', 'a'], [1, 2, 3])`` will
3
+ plot three points with x-axis values of 'd', 'f', 'a'.
5
4
6
5
See :doc:`/gallery/lines_bars_and_markers/categorical_variables` for an
7
6
example.
8
7
9
8
The module uses Matplotlib's `matplotlib.units` mechanism to convert from
10
- strings to integers, provides a tick locator and formatter, and the
11
- class: `.UnitData` that creates and stores the string-to-integer mapping.
9
+ strings to integers and provides a tick locator, a tick formatter, and the
10
+ `.UnitData` class that creates and stores the string-to-integer mapping.
12
11
"""
13
12
14
13
from collections import OrderedDict
29
28
class StrCategoryConverter (units .ConversionInterface ):
30
29
@staticmethod
31
30
def convert (value , unit , axis ):
32
- """Convert strings in value to floats using
33
- mapping information store in the unit object.
31
+ """
32
+ Convert strings in *value* to floats using mapping information stored
33
+ in the *unit* object.
34
34
35
35
Parameters
36
36
----------
@@ -39,7 +39,7 @@ def convert(value, unit, axis):
39
39
unit : `.UnitData`
40
40
An object mapping strings to integers.
41
41
axis : `~matplotlib.axis.Axis`
42
- axis on which the converted value is plotted.
42
+ The axis on which the converted value is plotted.
43
43
44
44
.. note:: *axis* is unused.
45
45
@@ -52,33 +52,27 @@ def convert(value, unit, axis):
52
52
'Missing category information for StrCategoryConverter; '
53
53
'this might be caused by unintendedly mixing categorical and '
54
54
'numeric data' )
55
-
56
55
# dtype = object preserves numerical pass throughs
57
56
values = np .atleast_1d (np .array (value , dtype = object ))
58
-
59
57
# pass through sequence of non binary numbers
60
- if all ((units .ConversionInterface .is_numlike (v ) and
61
- not isinstance (v , (str , bytes ))) for v in values ):
58
+ if all (units .ConversionInterface .is_numlike (v )
59
+ and not isinstance (v , (str , bytes ))
60
+ for v in values ):
62
61
return np .asarray (values , dtype = float )
63
-
64
62
# force an update so it also does type checking
65
63
unit .update (values )
66
-
67
- str2idx = np .vectorize (unit ._mapping .__getitem__ ,
68
- otypes = [float ])
69
-
70
- mapped_value = str2idx (values )
71
- return mapped_value
64
+ return np .vectorize (unit ._mapping .__getitem__ , otypes = [float ])(values )
72
65
73
66
@staticmethod
74
67
def axisinfo (unit , axis ):
75
- """Sets the default axis ticks and labels
68
+ """
69
+ Set the default axis ticks and labels.
76
70
77
71
Parameters
78
72
----------
79
73
unit : `.UnitData`
80
74
object string unit information for value
81
- axis : `~matplotlib.Axis. axis`
75
+ axis : `~matplotlib.axis.Axis `
82
76
axis for which information is being set
83
77
84
78
Returns
@@ -96,21 +90,21 @@ def axisinfo(unit, axis):
96
90
97
91
@staticmethod
98
92
def default_units (data , axis ):
99
- """Sets and updates the :class:`~matplotlib.Axis.axis` units.
93
+ """
94
+ Set and update the `~matplotlib.axis.Axis` units.
100
95
101
96
Parameters
102
97
----------
103
98
data : string or iterable of strings
104
- axis : `~matplotlib.Axis. axis`
99
+ axis : `~matplotlib.axis.Axis `
105
100
axis on which the data is plotted
106
101
107
102
Returns
108
103
-------
109
104
class : `.UnitData`
110
105
object storing string to integer mapping
111
106
"""
112
- # the conversion call stack is supposed to be
113
- # default_units->axis_info->convert
107
+ # the conversion call stack is default_units -> axis_info -> convert
114
108
if axis .units is None :
115
109
axis .set_units (UnitData (data ))
116
110
else :
@@ -119,13 +113,12 @@ class : `.UnitData`
119
113
120
114
121
115
class StrCategoryLocator (ticker .Locator ):
122
- """tick at every integer mapping of the string data"""
116
+ """Tick at every integer mapping of the string data. """
123
117
def __init__ (self , units_mapping ):
124
118
"""
125
119
Parameters
126
120
-----------
127
121
units_mapping : Dict[str, int]
128
- string:integer mapping
129
122
"""
130
123
self ._units = units_mapping
131
124
@@ -137,13 +130,12 @@ def tick_values(self, vmin, vmax):
137
130
138
131
139
132
class StrCategoryFormatter (ticker .Formatter ):
140
- """String representation of the data at every tick"""
133
+ """String representation of the data at every tick. """
141
134
def __init__ (self , units_mapping ):
142
135
"""
143
136
Parameters
144
137
----------
145
138
units_mapping : Dict[Str, int]
146
- string:integer mapping
147
139
"""
148
140
self ._units = units_mapping
149
141
@@ -156,8 +148,7 @@ def format_ticks(self, values):
156
148
157
149
@staticmethod
158
150
def _text (value ):
159
- """Converts text values into utf-8 or ascii strings.
160
- """
151
+ """Convert text values into utf-8 or ascii strings."""
161
152
if isinstance (value , bytes ):
162
153
value = value .decode (encoding = 'utf-8' )
163
154
elif not isinstance (value , str ):
@@ -183,8 +174,7 @@ def __init__(self, data=None):
183
174
@staticmethod
184
175
def _str_is_convertible (val ):
185
176
"""
186
- Helper method to see if a string can be cast to float or
187
- parsed as date.
177
+ Helper method to check whether a string can be parsed as float or date.
188
178
"""
189
179
try :
190
180
float (val )
@@ -196,7 +186,8 @@ def _str_is_convertible(val):
196
186
return True
197
187
198
188
def update (self , data ):
199
- """Maps new values to integer identifiers.
189
+ """
190
+ Map new values to integer identifiers.
200
191
201
192
Parameters
202
193
----------
0 commit comments