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

Skip to content

Commit ab03007

Browse files
committed
Remove some direct calls to atleast_1d.
Even if the argument to atleast_1d is array-compatible and has sufficient dimensions, it is still converted from its original type to a numpy array. This is overly aggressive for our purposes (especially unit support), so replace the direct call with a helper function that only calls atleast_1d if it's actually necessary.
1 parent e748de1 commit ab03007

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

0 commit comments

Comments
 (0)