8000 Merge pull request #8369 from anntzer/hist-reuses-_reshape_2D · matplotlib/matplotlib@73d2ea0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 73d2ea0

Browse files
authored
Merge pull request #8369 from anntzer/hist-reuses-_reshape_2D
Use cbook._reshape_2D in hist.
2 parents 199cea7 + 03e24b7 commit 73d2ea0

File tree

2 files changed

+13
-48
lines changed

2 files changed

+13
-48
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6109,41 +6109,6 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
61096109
bin_range = range
61106110
del range
61116111

6112-
def _normalize_input(inp, ename='input'):
6113-
"""Normalize 1 or 2d input into list of np.ndarray or
6114-
a single 2D np.ndarray.
6115-
6116-
Parameters
6117-
----------
6118-
inp : iterable
6119-
ename : str, optional
6120-
Name to use in ValueError if `inp` can not be normalized
6121-
6122-
"""
6123-
if (isinstance(x, np.ndarray) or
6124-
not iterable(cbook.safe_first_element(inp))):
6125-
# TODO: support masked arrays;
6126-
inp = np.asarray(inp)
6127-
if inp.ndim == 2:
6128-
# 2-D input with columns as datasets; switch to rows
6129-
inp = inp.T
6130-
elif inp.ndim == 1:
6131-
# new view, single row
6132-
inp = inp.reshape(1, inp.shape[0])
6133-
else:
6134-
raise ValueError(
6135-
"{ename} must be 1D or 2D".format(ename=ename))
6136-
if inp.shape[1] < inp.shape[0]:
6137-
warnings.warn(
6138-
'2D hist input should be nsamples x nvariables;\n '
6139-
'this looks transposed '
6140-
'(shape is %d x %d)' % inp.shape[::-1])
6141-
else:
6142-
# multiple hist with data of different length
6143-
inp = [np.asarray(xi) for xi in inp]
6144-
6145-
return inp
6146-
61476112
if not self._hold:
61486113
self.cla()
61496114

@@ -6178,20 +6143,18 @@ def _normalize_input(inp, ename='input'):
61786143
binsgiven = (cbook.iterable(bins) or bin_range is not None)
61796144

61806145
# basic input validation
6181-
flat = np.ravel(x)
6182-
6183-
input_empty = len(flat) == 0
6146+
input_empty = np.size(x) == 0
61846147

61856148
# Massage 'x' for processing.
61866149
if input_empty:
61876150
x = np.array([[]])
61886151
else:
6189-
x = _normalize_input(x, 'x')
6152+
x = cbook._reshape_2D(x, 'x')
61906153
nx = len(x) # number of datasets
61916154

61926155
# We need to do to 'weights' what was done to 'x'
61936156
if weights is not None:
6194-
w = _normalize_input(weights, 'weights')
6157+
w = cbook._reshape_2D(weights, 'weights')
61956158
else:
61966159
w = [None]*nx
61976160

lib/matplotlib/cbook/__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
17441744
bxpstats = []
17451745

17461746
# convert X to a list of lists
1747-
X = _reshape_2D(X)
1747+
X = _reshape_2D(X, "X")
17481748

17491749
ncols = len(X)
17501750
if labels is None:
@@ -1974,14 +1974,16 @@ def _check_1d(x):
19741974
return np.atleast_1d(x)
19751975

19761976

1977-
def _reshape_2D(X):
1977+
def _reshape_2D(X, name):
19781978
"""
1979-
Converts a non-empty list or an ndarray of two or fewer dimensions
1980-
into a list of iterable objects so that in
1979+
Use Fortran ordering to convert ndarrays and lists of iterables to lists of
1980+
1D arrays.
19811981
1982-
for v in _reshape_2D(X):
1982+
Lists of iterables are converted by applying `np.asarray` to each of their
1983+
elements. 1D ndarrays are returned in a singleton list containing them.
1984+
2D ndarrays are converted to the list of their *columns*.
19831985
1984-
v is iterable and can be used to instantiate a 1D array.
1986+
*name* is used to generate the error message for invalid inputs.
19851987
"""
19861988
# Iterate over columns for ndarrays, over rows otherwise.
19871989
X = X.T if isinstance(X, np.ndarray) else np.asarray(X)
@@ -1992,7 +1994,7 @@ def _reshape_2D(X):
19921994
# 2D array, or 1D array of iterables: flatten them first.
19931995
return [np.reshape(x, -1) for x in X]
19941996
else:
1995-
raise ValueError("input `X` must have 2 or fewer dimensions")
1997+
raise ValueError("{} must have 2 or fewer dimensions".format(name))
19961998

19971999

19982000
def violin_stats(X, method, points=100):
@@ -2039,7 +2041,7 @@ def violin_stats(X, method, points=100):
20392041
vpstats = []
20402042

20412043
# Want X to be a list of data sequences
2042-
X = _reshape_2D(X)
2044+
X = _reshape_2D(X, "X")
20432045

20442046
for x in X:
20452047
# Dictionary of results for this distribution

0 commit comments

Comments
 (0)
0