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

Skip to content

Commit c27970c

Browse files
committed
Merge pull request #4803 from dopplershift/fix-plot-units
FIX: unit support with `plot` and `pint`
2 parents b9bc7d1 + 9b65233 commit c27970c

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,7 @@ def tk_window_focus():
14651465
'matplotlib.tests.test_tightlayout',
14661466
'matplotlib.tests.test_transforms',
14671467
'matplotlib.tests.test_triangulation',
1468+
'matplotlib.tests.test_units',
14681469
'matplotlib.tests.test_widgets',
14691470
'matplotlib.tests.test_cycles',
14701471
'matplotlib.tests.test_labeled_data_unpacking',

lib/matplotlib/axes/_base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import matplotlib
1616

1717
from matplotlib import cbook
18-
from matplotlib.cbook import _string_to_bool, iterable, index_of, get_label
18+
from matplotlib.cbook import (_check_1d, _string_to_bool, iterable,
19+
index_of, get_label)
1920
from matplotlib import docstring
2021
import matplotlib.colors as mcolors
2122
import matplotlib.lines as mlines
@@ -214,8 +215,10 @@ def _xy_from_xy(self, x, y):
214215
if by:
215216
y = self.axes.convert_yunits(y)
216217

217-
x = np.atleast_1d(x) # like asanyarray, but converts scalar to array
218-
y = np.atleast_1d(y)
218+
# like asanyarray, but converts scalar to array, and doesn't change
219+
# existing compatible sequences
220+
x = _check_1d(x)
221+
y = _check_1d(y)
219222
if x.shape[0] != y.shape[0]:
220223
raise ValueError("x and y must have same first dimension")
221224
if x.ndim > 2 or y.ndim > 2:
@@ -353,8 +356,8 @@ def _plot_args(self, tup, kwargs):
353356
kwargs['label'] = get_label(tup[-1], None)
354357

355358
if len(tup) == 2:
356-
x = np.atleast_1d(tup[0])
357-
y = np.atleast_1d(tup[-1])
359+
x = _check_1d(tup[0])
360+
y = _check_1d(tup[-1])
358361
else:
359362
x, y = index_of(tup[-1])
360363

lib/matplotlib/cbook.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,21 @@ def is_math_text(s):
21922192
return even_dollars
21932193

21942194

2195+
def _check_1d(x):
2196+
'''
2197+
Converts a sequence of less than 1 dimension, to an array of 1
2198+
dimension; leaves everything else untouched.
2199+
'''
2200+
if not hasattr(x, 'shape') or len(x.shape) < 1:
2201+
return np.atleast_1d(x)
2202+
else:
2203+
try:
2204+
x[:, None]
2205+
return x
2206+
except (IndexError, TypeError):
2207+
return np.atleast_1d(x)
2208+
2209+
21952210
def _reshape_2D(X):
21962211
"""
21972212
Converts a non-empty list or an ndarray of two or fewer dimensions

lib/matplotlib/tests/test_units.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.units as munits
3+
import numpy as np
4+
5+
try:
6+
# mock in python 3.3+
7+
from unittest.mock import MagicMock
8+
except ImportError:
9+
from mock import MagicMock
10+
11+
12+
# Tests that the conversion machinery works properly for classes that
13+
# work as a facade over numpy arrays (like pint)
14+
def test_numpy_facade():
15+
# Basic class that wraps numpy array and has units
16+
class Quantity(object):
17+
def __init__(self, data, units):
18+
self.magnitude = data
19+
self.units = units
20+
21+
def to(self, new_units):
22+
return Quantity(self.magnitude, new_units)
23+
24+
def __getattr__(self, attr):
25+
return getattr(self.magnitude, attr)
26+
27+
def __getitem__(self, item):
28+
return self.magnitude[item]
29+
30+
# Create an instance of the conversion interface and
31+
# mock so we can check methods called
32+
qc = munits.ConversionInterface()
33+
34+
def convert(value, unit, axis):
35+
if hasattr(value, 'units'):
36+
return value.to(unit)
37+
else:
38+
return Quantity(value, axis.get_units()).to(unit).magnitude
39+
40+
qc.convert = MagicMock(side_effect=convert)
41+
qc.axisinfo = MagicMock(return_value=None)
42+
qc.default_units = MagicMock(side_effect=lambda x, a: x.units)
43+
44+
# Register the class
45+
munits.registry[Quantity] = qc
46+
47+
# Simple test
48+
t = Quantity(np.linspace(0, 10), 'sec')
49+
d = Quantity(30 * np.linspace(0, 10), 'm/s')
50+
51+
fig, ax = plt.subplots(1, 1)
52+
l, = plt.plot(t, d)
53+
ax.yaxis.set_units('inch')
54+
55+
assert qc.convert.called
56+
assert qc.axisinfo.called
57+
assert qc.default_units.called

0 commit comments

Comments
 (0)