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

Skip to content

Commit 9c37e3f

Browse files
committed
FIX: non-indexed pd.Series into hist
closes #5557
1 parent e48f7e2 commit 9c37e3f

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5875,6 +5875,40 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
58755875
.. plot:: mpl_examples/statistics/histogram_demo_features.py
58765876
58775877
"""
5878+
def _normalize_input(inp, ename='input'):
5879+
"""Normalize 1 or 2d input into list of np.ndarray or
5880+
a single 2D np.ndarray.
5881+
5882+
Parameters
5883+
----------
5884+
inp : iterable
5885+
ename : str, optional
5886+
Name to use is ValueError if can not be normalized
5887+
5888+
"""
5889+
if isinstance(x, np.ndarray) or not iterable(next(iter(inp))):
5890+
# TODO: support masked arrays;
5891+
inp = np.asarray(inp)
5892+
if inp.ndim == 2:
5893+
# 2-D input with columns as datasets; switch to rows
5894+
inp = inp.T
5895+
elif inp.ndim == 1:
5896+
# new view, single row
5897+
inp = inp.reshape(1, inp.shape[0])
5898+
else:
5899+
raise ValueError(
5900+
"{ename} must be 1D or 2D".format(ename=ename))
5901+
if inp.shape[1] < inp.shape[0]:
5902+
warnings.warn(
5903+
'2D hist input should be nsamples x nvariables;\n '
5904+
'this looks transposed '
5905+
'(shape is %d x %d)' % inp.shape[::-1])
5906+
else:
5907+
# multiple hist with data of different length
5908+
inp = [np.asarray(xi) for xi in inp]
5909+
5910+
return inp
5911+
58785912
if not self._hold:
58795913
self.cla()
58805914

@@ -5918,28 +5952,26 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
59185952
input_empty = len(flat) == 0
59195953

59205954
# Massage 'x' for processing.
5921-
# NOTE: Be sure any changes here is also done below to 'weights'
59225955
if input_empty:
59235956
x = np.array([[]])
5924-
elif isinstance(x, np.ndarray) or not iterable(x[0]):
5925-
# TODO: support masked arrays;
5926-
x = np.asarray(x)
5927-
if x.ndim == 2:
5928-
x = x.T # 2-D input with columns as datasets; switch to rows
5929-
elif x.ndim == 1:
5930-
x = x.reshape(1, x.shape[0]) # new view, single row
5931-
else:
5932-
raise ValueError("x must be 1D or 2D")
5933-
if x.shape[1] < x.shape[0]:
5934-
warnings.warn(
5935-
'2D hist input should be nsamples x nvariables;\n '
5936-
'this looks transposed (shape is %d x %d)' % x.shape[::-1])
59375957
else:
5938-
# multiple hist with data of different length
5939-
x = [np.asarray(xi) for xi in x]
5940-
5958+
x = _normalize_input(x, 'x')
59415959
nx = len(x) # number of datasets
59425960

5961+
# We need to do to 'weights' what was done to 'x'
5962+
if weights is not None:
5963+
w = _normalize_input(weights, 'weights')
5964+
else:
5965+
w = [None]*nx
5966+
5967+
if len(w) != nx:
5968+
raise ValueError('weights should have the same shape as x')
5969+
5970+
for xi, wi in zip(x, w):
5971+
if wi is not None and len(wi) != len(xi):
5972+
raise ValueError(
5973+
'weights should have the same shape as x')
5974+
59435975
if color is None and 'color' in self._get_lines._prop_keys:
59445976
color = [six.next(self._get_lines.prop_cycler)['color']
59455977
for i in xrange(nx)]
@@ -5948,28 +5980,6 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
59485980
if len(color) != nx:
59495981
raise ValueError("color kwarg must have one color per dataset")
59505982

5951-
# We need to do to 'weights' what was done to 'x'
5952-
if weights is not None:
5953-
if isinstance(weights, np.ndarray) or not iterable(weights[0]):
5954-
w = np.array(weights)
5955-
if w.ndim == 2:
5956-
w = w.T
5957-
elif w.ndim == 1:
5958-
w.shape = (1, w.shape[0])
5959-
else:
5960-
raise ValueError("weights must be 1D or 2D")
5961-
else:
5962-
w = [np.asarray(wi) for wi in weights]
5963-
5964-
if len(w) != nx:
5965-
raise ValueError('weights should have the same shape as x')
5966-
for i in xrange(nx):
5967-
if len(w[i]) != len(x[i]):
5968-
raise ValueError(
5969-
'weights should have the same shape as x')
5970-
else:
5971-
w = [None]*nx
5972-
59735983
# Save the datalimits for the same reason:
59745984
_saved_bounds = self.dataLim.bounds
59755985

0 commit comments

Comments
 (0)