Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 03e24b7

Browse filesBrowse files
committed
Use cbook._reshape_2D in hist.
... instead of rewriting essentially the same function locally. The warning regarding potentially transposed inputs could be restored if we consider it to be really worth it, but it probably belongs to `_reshape_2D` anyways as it also applies to box plots.
1 parent f836bc3 commit 03e24b7
Copy full SHA for 03e24b7

File tree

Expand file treeCollapse file tree

2 files changed

+13
-48
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-48
lines changed

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+3-40Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6106,41 +6106,6 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
61066106
bin_range = range
61076107
del range
61086108

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

@@ -6175,20 +6140,18 @@ def _normalize_input(inp, ename='input'):
61756140
binsgiven = (cbook.iterable(bins) or bin_range is not None)
61766141

61776142
# basic input validation
6178-
flat = np.ravel(x)
6179-
6180-
input_empty = len(flat) == 0
6143+
input_empty = np.size(x) == 0
61816144

61826145
# Massage 'x' for processing.
61836146
if input_empty:
61846147
x = np.array([[]])
61856148
else:
6186-
x = _normalize_input(x, 'x')
6149+
x = cbook._reshape_2D(x, 'x')
61876150
nx = len(x) # number of datasets
61886151

61896152
# We need to do to 'weights' what was done to 'x'
61906153
if weights is not None:
6191-
w = _normalize_input(weights, 'weights')
6154+
w = cbook._reshape_2D(weights, 'weights')
61926155
else:
61936156
w = [None]*nx
61946157

‎lib/matplotlib/cbook/__init__.py

Copy file name to clipboardExpand all lines: lib/matplotlib/cbook/__init__.py
+10-8Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.