11"""
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'.
54
65See :doc:`/gallery/lines_bars_and_markers/categorical_variables` for an
76example.
87
98The 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.
1211"""
1312
1413from collections import OrderedDict
2928class StrCategoryConverter (units .ConversionInterface ):
3029 @staticmethod
3130 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.
3434
3535 Parameters
3636 ----------
@@ -39,7 +39,7 @@ def convert(value, unit, axis):
3939 unit : `.UnitData`
4040 An object mapping strings to integers.
4141 axis : `~matplotlib.axis.Axis`
42- axis on which the converted value is plotted.
42+ The axis on which the converted value is plotted.
4343
4444 .. note:: *axis* is unused.
4545
@@ -52,33 +52,27 @@ def convert(value, unit, axis):
5252 'Missing category information for StrCategoryConverter; '
5353 'this might be caused by unintendedly mixing categorical and '
5454 'numeric data' )
55-
5655 # dtype = object preserves numerical pass throughs
5756 values = np .atleast_1d (np .array (value , dtype = object ))
58-
5957 # 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 ):
6261 return np .asarray (values , dtype = float )
63-
6462 # force an update so it also does type checking
6563 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 )
7265
7366 @staticmethod
7467 def axisinfo (unit , axis ):
75- """Sets the default axis ticks and labels
68+ """
69+ Set the default axis ticks and labels.
7670
7771 Parameters
7872 ----------
7973 unit : `.UnitData`
8074 object string unit information for value
81- axis : `~matplotlib.Axis. axis`
75+ axis : `~matplotlib.axis.Axis `
8276 axis for which information is being set
8377
8478 Returns
@@ -96,21 +90,21 @@ def axisinfo(unit, axis):
9690
9791 @staticmethod
9892 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.
10095
10196 Parameters
10297 ----------
10398 data : string or iterable of strings
104- axis : `~matplotlib.Axis. axis`
99+ axis : `~matplotlib.axis.Axis `
105100 axis on which the data is plotted
106101
107102 Returns
108103 -------
109104 class : `.UnitData`
110105 object storing string to integer mapping
111106 """
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
114108 if axis .units is None :
115109 axis .set_units (UnitData (data ))
116110 else :
@@ -119,13 +113,12 @@ class : `.UnitData`
119113
120114
121115class StrCategoryLocator (ticker .Locator ):
122- """tick at every integer mapping of the string data"""
116+ """Tick at every integer mapping of the string data. """
123117 def __init__ (self , units_mapping ):
124118 """
125119 Parameters
126120 -----------
127121 units_mapping : Dict[str, int]
128- string:integer mapping
129122 """
130123 self ._units = units_mapping
131124
@@ -137,13 +130,12 @@ def tick_values(self, vmin, vmax):
137130
138131
139132class StrCategoryFormatter (ticker .Formatter ):
140- """String representation of the data at every tick"""
133+ """String representation of the data at every tick. """
141134 def __init__ (self , units_mapping ):
142135 """
143136 Parameters
144137 ----------
145138 units_mapping : Dict[Str, int]
146- string:integer mapping
147139 """
148140 self ._units = units_mapping
149141
@@ -156,8 +148,7 @@ def format_ticks(self, values):
156148
157149 @staticmethod
158150 def _text (value ):
159- """Converts text values into utf-8 or ascii strings.
160- """
151+ """Convert text values into utf-8 or ascii strings."""
161152 if isinstance (value , bytes ):
162153 value = value .decode (encoding = 'utf-8' )
163154 elif not isinstance (value , str ):
@@ -183,8 +174,7 @@ def __init__(self, data=None):
183174 @staticmethod
184175 def _str_is_convertible (val ):
185176 """
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.
188178 """
189179 try :
190180 float (val )
@@ -196,7 +186,8 @@ def _str_is_convertible(val):
196186 return True
197187
198188 def update (self , data ):
199- """Maps new values to integer identifiers.
189+ """
190+ Map new values to integer identifiers.
200191
201192 Parameters
202193 ----------
0 commit comments