Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cfdbd9b

Browse files
committed
addressing documentation comments + more use of units
1 parent 543d235 commit cfdbd9b

File tree

7 files changed

+118
-68
lines changed

7 files changed

+118
-68
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ install:
6666
- activate test-environment
6767
- echo %PYTHON_VERSION% %TARGET_ARCH%
6868
# pytest-cov>=2.3.1 due to https://github.com/pytest-dev/pytest-cov/issues/124
69-
- pip install -q "pytest!=3.3.0" "pytest-cov>=2.3.1" pytest-rerunfailures pytest-timeout pytest-xdist
69+
- pip install -q "pytest!=3.3.0,>=3.2.0" "pytest-cov>=2.3.1" pytest-rerunfailures pytest-timeout pytest-xdist
7070

7171
# Apply patch to `subprocess` on Python versions > 2 and < 3.6.3
7272
# https://github.com/matplotlib/matplotlib/issues/9176

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ env:
5252
- NUMPY=numpy
5353
- PANDAS=
5454
- PYPARSING=pyparsing
55-
- PYTEST=pytest!=3.3.0
55+
- PYTEST='pytest!=3.3.0,>=3.2.0'
5656
- PYTEST_COV=pytest-cov
5757
- PYTEST_PEP8=
5858
- SPHINX=sphinx
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Deprecated `Axis.unt_data`
2+
``````````````````````````
3+
4+
Use `Axis.units` (which has long existed) instead.
5+
6+
Only accept string-like for Categorical input
7+
`````````````````````````````````````````````
8+
9+
Do not accept mixed string / float / int input, only
10+
strings are valid categoricals.

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6442,7 +6442,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64426442
if normed is not None:
64436443
warnings.warn("The 'normed' kwarg is deprecated, and has been "
64446444
"replaced by the 'density' kwarg.")
6445-
6445+
64466446
# basic input validation
64476447
input_empty = np.size(x) == 0
64486448
# Massage 'x' for processing.

lib/matplotlib/axis.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,6 @@ def __init__(self, axes, pickradius=15):
720720
self.labelpad = rcParams['axes.labelpad']
721721
self.offsetText = self._get_offset_text()
722722

723-
self.majorTicks = []
724-
self.minorTicks = []
725-
726723
self.pickradius = pickradius
727724

728725
# Initialize here for testing; later add API
@@ -780,14 +777,14 @@ def limit_range_for_scale(self, vmin, vmax):
780777
return self._scale.limit_range_for_scale(vmin, vmax, self.get_minpos())
781778

782779
@property
783-
@cbook.deprecated("2.1.1")
780+
@cbook.deprecated("2.2.0")
784781
def unit_data(self):
785-
return self._units
782+
return self.units
786783

787784
@unit_data.setter
788-
@cbook.deprecated("2.1.1")
785+
@cbook.deprecated("2.2.0")
789786
def unit_data(self, unit_data):
790-
self.set_units = unit_data
787+
self.set_units(unit_data)
791788

792789
def get_children(self):
793790
children = [self.label, self.offsetText]

lib/matplotlib/category.py

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
catch all for categorical functions
3+
StrCategorical module for facilitating natively plotting String/Text data.
4+
This module contains the conversion mechanism (a monotonic mapping from
5+
strings to integers), tick locator and formatter, and the class:`.UnitData`
6+
object that creates and stores the string to integer mapping.
47
"""
58
from __future__ import (absolute_import, division, print_function,
69
unicode_literals)
710

8-
from collections import Iterable, OrderedDict
11+
from collections import OrderedDict
912
import itertools
1013

1114
import six
1215

16+
1317
import numpy as np
1418

1519
import matplotlib.units as units
@@ -22,31 +26,26 @@
2226
(bytes, six.text_type, np.str_, np.bytes_)))
2327

2428

25-
def to_str(value):
26-
"""Helper function to turn values to strings.
27-
"""
28-
# Note: This function is only used by StrCategoryFormatter
29-
if LooseVersion(np.__version__) < LooseVersion('1.7.0'):
30-
if (isinstance(value, (six.text_type, np.unicode))):
31-
value = value.encode('utf-8', 'ignore').decode('utf-8')
32-
if isinstance(value, (np.bytes_, six.binary_type)):
33-
value = value.decode(encoding='utf-8')
34-
elif not isinstance(value, (np.str_, six.string_types)):
35-
value = str(value)
36-
return value
37-
38-
3929
class StrCategoryConverter(units.ConversionInterface):
4030
@staticmethod
4131
def convert(value, unit, axis):
42-
"""Uses axis.units to encode string data as floats
32+
"""Converts strings in value to floats using
33+
mapping information store in the unit object
4334
4435
Parameters
4536
----------
46-
value: string, iterable
47-
value or list of values to plot
48-
unit:
49-
axis:
37+
value : string or iterable
38+
value or list of values to be converted
39+
unit : :class:`.UnitData`
40+
object string unit information for value
41+
axis : :class:`~matplotlib.Axis.axis`
42+
axis on which the converted value is plotted
43+
44+
Returns
45+
-------
46+
mapped_ value : float or ndarray of floats
47+
48+
.. note:: axis is not used in this function
5049
"""
5150
# dtype = object preserves numerical pass throughs
5251
values = np.atleast_1d(np.array(value, dtype=object))
@@ -57,26 +56,53 @@ def convert(value, unit, axis):
5756
return np.asarray(values, dtype=float)
5857

5958
# force an update so it also does type checking
60-
axis.units.update(values)
59+
unit.update(values)
6160

62-
str2idx = np.vectorize(axis.units._mapping.__getitem__,
61+
str2idx = np.vectorize(unit._mapping.__getitem__,
6362
otypes=[float])
6463

6564
mapped_value = str2idx(values)
6665
return mapped_value
6766

6867
@staticmethod
6968
def axisinfo(unit, axis):
70-
"""Sets the axis ticks and labels
69+
"""Sets the default axis ticks and labels
70+
71+
Parameters
72+
---------
73+
unit : :class:`.UnitData`
74+
object string unit information for value
75+
axis : :class:`~matplotlib.Axis.axis`
76+
axis for which information is being set
77+
78+
Returns
79+
-------
80+
:class:~matplotlib.units.AxisInfo~
81+
Information to support default tick labeling
82+
83+
.. note: axis is not used
7184
"""
7285
# locator and formatter take mapping dict because
7386
# args need to be pass by reference for updates
74-
majloc = StrCategoryLocator(axis.units)
75-
majfmt = StrCategoryFormatter(axis.units)
87+
majloc = StrCategoryLocator(unit._mapping)
88+
majfmt = StrCategoryFormatter(unit._mapping)
7689
return units.AxisInfo(majloc=majloc, majfmt=majfmt)
7790

7891
@staticmethod
79-
def default_units(data=None, axis=None):
92+
def default_units(data, axis):
93+
""" Sets and updates the :class:`~matplotlib.Axis.axis~ units
94+
95+
Parameters
96+
----------
97+
data : string or iterable of strings
98+
axis : :class:`~matplotlib.Axis.axis`
99+
axis on which the data is plotted
100+
101+
Returns
102+
-------
103+
class:~.UnitData~
104+
object storing string to integer mapping
105+
"""
80106
# the conversion call stack is supposed to be
81107
# default_units->axis_info->convert
82108
if axis.units is None:
@@ -88,39 +114,53 @@ def default_units(data=None, axis=None):
88114

89115
class StrCategoryLocator(ticker.Locator):
90116
"""tick at every integer mapping of the string data"""
91-
def __init__(self, units):
117+
def __init__(self, units_mapping):
92118
"""
93119
Parameters
94120
-----------
95121
units: dict
96-
(string, integer) mapping
122+
string:integer mapping
97123
"""
98-
self._units = units
124+
self._units = units_mapping
99125

100126
def __call__(self):
101-
return list(self._units._mapping.values())
127+
return list(self._units.values())
102128

103129
def tick_values(self, vmin, vmax):
104130
return self()
105131

106132

107133
class StrCategoryFormatter(ticker.Formatter):
108134
"""String representation of the data at every tick"""
109-
def __init__(self, units):
135+
def __init__(self, units_mapping):
110136
"""
111137
Parameters
112138
----------
113139
units: dict
114-
(string, integer) mapping
140+
string:integer mapping
115141
"""
116-
self._units = units
142+
self._units = units_mapping
117143

118144
def __call__(self, x, pos=None):
119145
if pos is None:
120146
return ""
121-
r_mapping = {v: to_str(k) for k, v in self._units._mapping.items()}
147+
r_mapping = {v: StrCategoryFormatter.__text__(k)
148+
for k, v in self._units.items()}
122149
return r_mapping.get(int(np.round(x)), '')
123150

151+
@staticmethod
152+
def __text__(value):
153+
"""Converts text values into `utf-8` or `ascii` strings
154+
"""
155+
if LooseVersion(np.__version__) < LooseVersion('1.7.0'):
156+
if (isinstance(value, (six.text_type, np.unicode))):
157+
value = value.encode('utf-8', 'ignore').decode('utf-8')
158+
if isinstance(value, (np.bytes_, six.binary_type)):
159+
value = value.decode(encoding='utf-8')
160+
elif not isinstance(value, (np.str_, six.string_types)):
161+
value = str(value)
162+
return value
163+
124164

125165
class UnitData(object):
126166
def __init__(self, data=None):
@@ -130,11 +170,10 @@ def __init__(self, data=None):
130170
data: iterable
131171
sequence of string values
132172
"""
133-
if data is None:
134-
data = ()
135173
self._mapping = OrderedDict()
136174
self._counter = itertools.count(start=0)
137-
self.update(data)
175+
if data is not None:
176+
self.update(data)
138177

139178
def update(self, data):
140179
"""Maps new values to integer identifiers.
@@ -149,13 +188,9 @@ def update(self, data):
149188
TypeError
150189
If the value in data is not a string, unicode, bytes type
151190
"""
191+
data = np.atleast_1d(np.array(data, dtype=object))
152192

153-
if (isinstance(data, VALID_TYPES) or
154-
not isinstance(data, Iterable)):
155-
data = [data]
156-
157-
unsorted_unique = OrderedDict.fromkeys(data)
158-
for val in unsorted_unique:
193+
for val in OrderedDict.fromkeys(data):
159194
if not isinstance(val, VALID_TYPES):
160195
raise TypeError("{val!r} is not a string".format(val=val))
161196
if val not in self._mapping:

0 commit comments

Comments
 (0)