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

Skip to content
Merged
26 changes: 15 additions & 11 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7416,7 +7416,14 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,

# Massage 'x' for processing.
x = cbook._reshape_2D(x, 'x')
nx = len(x) # number of datasets
nx = len(x)
Comment thread
timhoffm marked this conversation as resolved.
Outdated

for arr in x:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I'm not in favor of checking for all kinds of erronous inputs. This is not good, but it's possibly the least bad option (other than making timedelta work properlyI) because timedelta is an accepted input in other cases.

if hasattr(arr, "dtype") and np.issubdtype(arr.dtype, np.timedelta64):
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated
raise TypeError(
"ax.hist does not currently support numpy.timedelta64."
"Convert to numeric values (e.g., .total_seconds()) first."
)

# Process unit information. _process_unit_info sets the unit and
# converts the first dataset; then we convert each following dataset
Expand Down Expand Up @@ -7470,16 +7477,13 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
# does not do this for us when guessing the range (but will
# happily ignore nans when computing the histogram).
if bin_range is None:
xmin = np.inf
xmax = -np.inf
for xi in x:
if len(xi):
# python's min/max ignore nan,
# np.minnan returns nan for all nan input
xmin = min(xmin, np.nanmin(xi))
xmax = max(xmax, np.nanmax(xi))
if xmin <= xmax: # Only happens if we have seen a finite value.
bin_range = (xmin, xmax)
finite_data = [xi for xi in x if len(xi)]
if finite_data:
xmin = min(np.nanmin(xi) for xi in finite_data)
xmax = max(np.nanmax(xi) for xi in finite_data)
bin_range = (xmin, xmax)
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated
else:
bin_range = None
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated

# If bins are not specified either explicitly or via range,
# we need to figure out the range required for all datasets,
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,17 @@ def test_hist_log_barstacked():
assert axs[0].get_ylim() == axs[1].get_ylim()


def test_hist_timedelta_raises():
import numpy as np
import matplotlib.pyplot as plt

arr = np.array([1,2,5,7], dtype="timedelta64[D]")
fig, ax = plt.subplots()

with pytest.raises(TypeError, match="does not currently support numpy.timedelta64"):
ax.hist(arr)

Comment thread
timhoffm marked this conversation as resolved.

@image_comparison(['hist_bar_empty.png'], remove_text=True)
def test_hist_bar_empty():
# From #3886: creating hist from empty dataset raises ValueError
Expand Down
Loading