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

Skip to content

Commit a7cc2bf

Browse files
authored
Merge pull request #14606 from anntzer/histclean
Generic cleanup to hist().
2 parents 071da48 + 491f759 commit a7cc2bf

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6528,18 +6528,16 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
65286528

65296529
for xi, wi in zip(x, w):
65306530
if wi is not None and len(wi) != len(xi):
6531-
raise ValueError(
6532-
'weights should have the same shape as x')
6531+
raise ValueError('weights should have the same shape as x')
65336532

65346533
if color is None:
65356534
color = [self._get_lines.get_next_color() for i in range(nx)]
65366535
else:
65376536
color = mcolors.to_rgba_array(color)
65386537
if len(color) != nx:
6539-
error_message = (
6540-
"color kwarg must have one color per data set. %d data "
6541-
"sets and %d colors were provided" % (nx, len(color)))
6542-
raise ValueError(error_message)
6538+
raise ValueError(f"The 'color' keyword argument must have one "
6539+
f"color per dataset, but {nx} datasets and "
6540+
f"{len(color)} colors were provided")
65436541

65446542
hist_kwargs = dict()
65456543

@@ -6555,9 +6553,7 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
65556553
# np.minnan returns nan for all nan input
65566554
xmin = min(xmin, np.nanmin(xi))
65576555
xmax = max(xmax, np.nanmax(xi))
6558-
# make sure we have seen at least one non-nan and finite
6559-
# value before we reset the bin range
6560-
if not np.isnan([xmin, xmax]).any() and not (xmin > xmax):
6556+
if xmin <= xmax: # Only happens if we have seen a finite value.
65616557
bin_range = (xmin, xmax)
65626558

65636559
# If bins are not specified either explicitly or via range,
@@ -6667,9 +6663,9 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
66676663
x[2*len(bins)-1:] = x[1:2*len(bins)-1][::-1]
66686664

66696665
if bottom is None:
6670-
bottom = np.zeros(len(bins) - 1)
6666+
bottom = 0
66716667

6672-
y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = bottom, bottom
6668+
y[1:2*len(bins)-1:2] = y[2:2*len(bins):2] = bottom
66736669
y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1]
66746670

66756671
if log:
@@ -6693,8 +6689,7 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
66936689
# top of the previous polygon becomes the bottom
66946690
y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1]
66956691
# set the top of this polygon
6696-
y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = (m + bottom,
6697-
m + bottom)
6692+
y[1:2*len(bins)-1:2] = y[2:2*len(bins):2] = m + bottom
66986693

66996694
# The starting point of the polygon has not yet been
67006695
# updated. So far only the endpoint was adjusted. This
@@ -6735,22 +6730,15 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
67356730
self.set_autoscaley_on(_saved_autoscaley)
67366731
self._request_autoscale_view()
67376732

6738-
if label is None:
6739-
labels = [None]
6740-
elif isinstance(label, str):
6741-
labels = [label]
6742-
elif not np.iterable(label):
6743-
labels = [str(label)]
6744-
else:
6745-
labels = [str(lab) for lab in label]
6746-
6733+
# If None, make all labels None (via zip_longest below); otherwise,
6734+
# cast each element to str, but keep a single str as it.
6735+
labels = [] if label is None else np.atleast_1d(np.asarray(label, str))
67476736
for patch, lbl in itertools.zip_longest(patches, labels):
67486737
if patch:
67496738
p = patch[0]
67506739
p.update(kwargs)
67516740
if lbl is not None:
67526741
p.set_label(lbl)
6753-
67546742
for p in patch[1:]:
67556743
p.update(kwargs)
67566744
p.set_label('_nolegend_')

0 commit comments

Comments
 (0)