1- # -*- coding: utf-8 -*-
21"""
32Module that allows plotting of string "category" data. i.e.
43``plot(['d', 'f', 'a'],[1, 2, 3])`` will plot three points with x-axis
1110strings to integers, provides a tick locator and formatter, and the
1211class:`.UnitData` that creates and stores the string-to-integer mapping.
1312"""
14- from __future__ import (absolute_import , division , print_function ,
15- unicode_literals )
1613
1714from collections import OrderedDict
1815import itertools
1916
20- import six
21-
22-
2317import numpy as np
2418
2519import matplotlib .units as units
2620import matplotlib .ticker as ticker
2721
28- # np 1.6/1.7 support
29- from distutils .version import LooseVersion
30-
31- VALID_TYPES = tuple (set (six .string_types +
32- (bytes , six .text_type , np .str_ , np .bytes_ )))
33-
3422
3523class StrCategoryConverter (units .ConversionInterface ):
3624 @staticmethod
@@ -58,7 +46,7 @@ def convert(value, unit, axis):
5846
5947 # pass through sequence of non binary numbers
6048 if all ((units .ConversionInterface .is_numlike (v ) and
61- not isinstance (v , VALID_TYPES )) for v in values ):
49+ not isinstance (v , ( str , bytes ) )) for v in values ):
6250 return np .asarray (values , dtype = float )
6351
6452 # force an update so it also does type checking
@@ -96,7 +84,7 @@ def axisinfo(unit, axis):
9684
9785 @staticmethod
9886 def default_units (data , axis ):
99- """ Sets and updates the :class:`~matplotlib.Axis.axis~ units
87+ """Sets and updates the :class:`~matplotlib.Axis.axis` units.
10088
10189 Parameters
10290 ----------
@@ -156,28 +144,27 @@ def __call__(self, x, pos=None):
156144
157145 @staticmethod
158146 def _text (value ):
159- """Converts text values into ` utf-8` or ` ascii` strings
147+ """Converts text values into utf-8 or ascii strings.
160148 """
161- if LooseVersion (np .__version__ ) < LooseVersion ('1.7.0' ):
162- if (isinstance (value , (six .text_type , np .unicode ))):
163- value = value .encode ('utf-8' , 'ignore' ).decode ('utf-8' )
164- if isinstance (value , (np .bytes_ , six .binary_type )):
149+ if isinstance (value , bytes ):
165150 value = value .decode (encoding = 'utf-8' )
166- elif not isinstance (value , ( np . str_ , six . string_types ) ):
151+ elif not isinstance (value , str ):
167152 value = str (value )
168153 return value
169154
170155
171156class UnitData (object ):
172157 def __init__ (self , data = None ):
173- """Create mapping between unique categorical values
174- and integer identifiers
158+ """
159+ Create mapping between unique categorical values and integer ids.
160+
161+ Parameters
175162 ----------
176163 data: iterable
177164 sequence of string values
178165 """
179166 self ._mapping = OrderedDict ()
180- self ._counter = itertools .count (start = 0 )
167+ self ._counter = itertools .count ()
181168 if data is not None :
182169 self .update (data )
183170
@@ -197,7 +184,7 @@ def update(self, data):
197184 data = np .atleast_1d (np .array (data , dtype = object ))
198185
199186 for val in OrderedDict .fromkeys (data ):
200- if not isinstance (val , VALID_TYPES ):
187+ if not isinstance (val , ( str , bytes ) ):
201188 raise TypeError ("{val!r} is not a string" .format (val = val ))
202189 if val not in self ._mapping :
203190 self ._mapping [val ] = next (self ._counter )
@@ -206,6 +193,5 @@ def update(self, data):
206193# Connects the convertor to matplotlib
207194units .registry [str ] = StrCategoryConverter ()
208195units .registry [np .str_ ] = StrCategoryConverter ()
209- units .registry [six .text_type ] = StrCategoryConverter ()
210196units .registry [bytes ] = StrCategoryConverter ()
211197units .registry [np .bytes_ ] = StrCategoryConverter ()
0 commit comments