Skip to content

Commit 239be7b

Browse files
committed
MNT: copy logic from numpy
as suggested by @eric-wieser We are no longer tracking if the bins kwarg was passed, but if it was passed in is an array we should use as the bin edges. Simplify some internal logic.
1 parent 5892d62 commit 239be7b

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

lib/matplotlib/axes/_axes.py

+34-15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@
3636
from matplotlib.axes._base import _AxesBase, _process_plot_format
3737
from matplotlib.axes._secondary_axes import SecondaryAxis
3838

39+
try:
40+
from numpy.lib.histograms import histogram_bin_edges
41+
except ImportError:
42+
def histogram_bin_edges(arr, bins, range=None, weights=None):
43+
if isinstance(bins, str):
44+
# rather than backporting the internals, just do the full
45+
# computation. If this is too slow for users, they can
46+
# update numpy, or pick a manual number of bins
47+
return np.histogram(arr, bins, range, weights)[1]
48+
else:
49+
if bins is None:
50+
# hard-code numpy's default
51+
bins = 10
52+
if range is None:
53+
range = np.min(arr), np.max(arr)
54+
55+
return np.linspace(*range, bins + 1)
56+
57+
3958
_log = logging.getLogger(__name__)
4059

4160

@@ -6611,10 +6630,8 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
66116630
if bin_range is not None:
66126631
bin_range = self.convert_xunits(bin_range)
66136632

6614-
# Check whether bins or range are given explicitly.
6615-
binsgiven = ((np.iterable(bins) and
6616-
not isinstance(bins, str)) or
6617-
bin_range is not None)
6633+
# this in True for 1D arrays, and False for None and str
6634+
bins_array_given = np.ndim(bins) == 1
66186635

66196636
# We need to do to 'weights' what was done to 'x'
66206637
if weights is not None:
@@ -6640,22 +6657,24 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
66406657
"sets and %d colors were provided" % (nx, len(color)))
66416658
raise ValueError(error_message)
66426659

6660+
hist_kwargs = dict()
6661+
66436662
# If bins are not specified either explicitly or via range,
66446663
# we need to figure out the range required for all datasets,
66456664
# and supply that to np.histogram.
6646-
if not binsgiven and not input_empty:
6647-
xmin = np.inf
6648-
xmax = -np.inf
6649-
for xi in x:
6650-
if len(xi) > 0:
6651-
xmin = min(xmin, np.nanmin(xi))
6652-
xmax = max(xmax, np.nanmax(xi))
6653-
bin_range = (xmin, xmax)
6665+
if not bins_array_given and not input_empty and len(x) > 1:
6666+
if weights is not None:
6667+
_w = np.concatenate(w)
6668+
else:
6669+
_w = None
6670+
bins = histogram_bin_edges(np.concatenate(x),
6671+
bins, bin_range, _w)
6672+
else:
6673+
hist_kwargs['range'] = bin_range
6674+
66546675
density = bool(density) or bool(normed)
66556676
if density and not stacked:
6656-
hist_kwargs = dict(range=bin_range, density=density)
6657-
else:
6658-
hist_kwargs = dict(range=bin_range)
6677+
hist_kwargs = dict(density=density)
66596678

66606679
# List to store all the top coordinates of the histograms
66616680
tops = []

0 commit comments

Comments
 (0)