Skip to content

Commit 20cb005

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 ed44362 commit 20cb005

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/matplotlib/axes/_base.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import matplotlib
1616

1717
from matplotlib import cbook
18-
from matplotlib.cbook import _string_to_bool
18+
from matplotlib.cbook import _check_1d, _string_to_bool
1919
from matplotlib import docstring
2020
import matplotlib.colors as mcolors
2121
import matplotlib.lines as mlines
@@ -216,8 +216,10 @@ def _xy_from_xy(self, x, y):
216216
if by:
217217
y = self.axes.convert_yunits(y)
218218

219-
x = np.atleast_1d(x) # like asanyarray, but converts scalar to array
220-
y = np.atleast_1d(y)
219+
# like asanyarray, but converts scalar to array, and doesn't change
220+
# existing compatible sequences
221+
x = _check_1d(x)
222+
y = _check_1d(y)
221223
if x.shape[0] != y.shape[0]:
222224
raise ValueError("x and y must have same first dimension")
223225
if x.ndim > 2 or y.ndim > 2:
@@ -277,10 +279,10 @@ def _plot_args(self, tup, kwargs):
277279
if v is not None:
278280
kw[k] = v
279281

280-
y = np.atleast_1d(tup[-1])
282+
y = _check_1d(tup[-1])
281283

282284
if len(tup) == 2:
283-
x = np.atleast_1d(tup[0])
285+
x = _check_1d(tup[0])
284286
else:
285287
x = np.arange(y.shape[0], dtype=float)
286288

lib/matplotlib/cbook.py

+15
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)