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

Skip to content

Commit 7013b6c

Browse files
committed
Proof of concept "accepts units" decorator
1 parent 2392dac commit 7013b6c

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

.travis.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@ env:
6565

6666
matrix:
6767
include:
68-
- python: 2.7
69-
# pytest-cov>=2.3.1 due to https://github.com/pytest-dev/pytest-cov/issues/124.
70-
env:
71-
- CYCLER=cycler==0.10
72-
- DATEUTIL=python-dateutil==2.1
73-
- MOCK=mock
74-
- NOSE=nose
75-
- NUMPY=numpy==1.7.1
76-
- PANDAS='pandas<0.21.0'
77-
- PYPARSING=pyparsing==2.0.1
78-
- PYTEST=pytest==3.1.0
79-
- PYTEST_COV=pytest-cov==2.3.1
80-
- SPHINX=sphinx==1.3
8168
- python: 3.4
8269
env: PYTHON_ARGS=-OO
8370
- python: 3.6

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,7 +4059,7 @@ def dopatch(xs, ys, **kwargs):
40594059
return dict(whiskers=whiskers, caps=caps, boxes=boxes,
40604060
medians=medians, fliers=fliers, means=means)
40614061

4062-
@munits._accepts_units(self, convert_x=[1], convert_y=[2])
4062+
@munits._accepts_units(convert_x=['x'], convert_y=['y'])
40634063
@_preprocess_data(replace_names=["x", "y", "s", "linewidths",
40644064
"edgecolors", "c", "facecolor",
40654065
"facecolors", "color"],
@@ -4211,7 +4211,6 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
42114211
if edgecolors is None and not rcParams['_internal.classic_mode']:
42124212
edgecolors = 'face'
42134213

4214-
42154214
# np.ma.ravel yields an ndarray, not a masked array,
42164215
# unless its argument is a masked array.
42174216
xy_shape = (np.shape(x), np.shape(y))

lib/matplotlib/units.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,68 @@ def default_units(x, axis):
4444
from __future__ import (absolute_import, division, print_function,
4545
unicode_literals)
4646

47-
47+
import inspect
4848
import six
4949
from matplotlib.cbook import iterable, is_numlike, safe_first_element
5050
import numpy as np
5151

5252

53+
def _accepts_units(convert_x, convert_y):
54+
"""
55+
A decorator for functions and methods that accept units. The parameters
56+
indicated in *convert_x* and *convert_y* are used to update the axis
57+
unit information, are converted, and then handed on to the decorated
58+
function.
59+
60+
Parameters
61+
----------
62+
convert_x, convert_y : list
63+
A list of integers or strings, indicating the arguments to be converted
64+
"""
65+
def decorator(func):
66+
def wrapper(*args, **kwargs):
67+
axes = args[0]
68+
bound_args = inspect.signature(func).bind(*args, **kwargs)
69+
arguments = bound_args.arguments
70+
has_data = (('data' in arguments) and
71+
(arguments['data'] is not None))
72+
if has_data:
73+
data = arguments['data']
74+
75+
for arg in convert_x:
76+
if has_data and arg in data:
77+
original_data = data[arg]
78+
axes._process_unit_info(xdata=original_data, kwargs=kwargs)
79+
converted_data = axes.convert_xunits(original_data)
80+
data[arg] = converted_data
81+
else:
82+
original_data = arguments[arg]
83+
axes._process_unit_info(xdata=original_data, kwargs=kwargs)
84+
converted_data = axes.convert_xunits(original_data)
85+
arguments[arg] = converted_data
86+
87+
for arg in convert_y:
88+
if has_data and arg in data:
89+
original_data = data[arg]
90+
axes._process_unit_info(ydata=original_data, kwargs=kwargs)
91+
converted_data = axes.convert_yunits(original_data)
92+
data[arg] = converted_data
93+
else:
94+
original_data = arguments[arg]
95+
axes._process_unit_info(ydata=original_data, kwargs=kwargs)
96+
converted_data = axes.convert_yunits(original_data)
97+
arguments[arg] = converted_data
98+
99+
if has_data:
100+
arguments['data'] = data
101+
bound_args.arguments = arguments
102+
args = bound_args.args
103+
kwargs = bound_args.kwargs
104+
return func(*args, **kwargs)
105+
return wrapper
106+
return decorator
107+
108+
53109
class AxisInfo(object):
54110
"""
55111
Information to support default axis labeling, tick labeling, and

0 commit comments

Comments
 (0)