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

Skip to content

Commit a09798e

Browse files
committed
g# This is a combination of 3 commits.
Correctly convert units for a stacked histogram Make sure reshaping is done before converting units Use list comprehension
1 parent c465993 commit a09798e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5879,7 +5879,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
58795879
Parameters
58805880
----------
58815881
x : (n,) array or sequence of (n,) arrays
5882-
Input values, this takes either a single array or a sequency of
5882+
Input values, this takes either a single array or a sequence of
58835883
arrays which are not required to be of the same length
58845884
58855885
bins : integer or sequence or 'auto', optional
@@ -6091,30 +6091,36 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
60916091
"Please only use 'density', since 'normed'"
60926092
"will be deprecated.")
60936093

6094-
# process the unit information
6095-
self._process_unit_info(xdata=x, kwargs=kwargs)
6096-
x = self.convert_xunits(x)
6097-
if bin_range is not None:
6098-
bin_range = self.convert_xunits(bin_range)
6099-
6100-
# Check whether bins or range are given explicitly.
6101-
binsgiven = (cbook.iterable(bins) or bin_range is not None)
6102-
61036094
# basic input validation
61046095
input_empty = np.size(x) == 0
6105-
61066096
# Massage 'x' for processing.
61076097
if input_empty:
61086098
x = np.array([[]])
61096099
else:
61106100
x = cbook._reshape_2D(x, 'x')
61116101
nx = len(x) # number of datasets
61126102

6103+
# Process unit information
6104+
# If doing a stacked histogram, the input is a list of datasets, so
6105+
# we need to do the unit conversion individually on each dataset
6106+
if stacked:
6107+
self._process_unit_info(xdata=x[0], kwargs=kwargs)
6108+
x = [self.convert_xunits(xi) for xi in x]
6109+
else:
6110+
self._process_unit_info(xdata=x, kwargs=kwargs)
6111+
x = self.convert_xunits(x)
6112+
6113+
if bin_range is not None:
6114+
bin_range = self.convert_xunits(bin_range)
6115+
6116+
# Check whether bins or range are given explicitly.
6117+
binsgiven = (cbook.iterable(bins) or bin_range is not None)
6118+
61136119
# We need to do to 'weights' what was done to 'x'
61146120
if weights is not None:
61156121
w = cbook._reshape_2D(weights, 'weights')
61166122
else:
6117-
w = [None]*nx
6123+
w = [None] * nx
61186124

61196125
if len(w) != nx:
61206126
raise ValueError('weights should have the same shape as x')

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,13 @@ def test_hist_unequal_bins_density():
15761576
assert_allclose(mpl_heights, np_heights)
15771577

15781578

1579+
def test_hist_stacked_datetime():
1580+
data = [[datetime.datetime(2017, 1, 1), datetime.datetime(2017, 1, 1)],
1581+
[datetime.datetime(2017, 1, 1), datetime.datetime(2017, 1, 2)]]
1582+
fig, ax = plt.subplots()
1583+
ax.hist(data, stacked=True)
1584+
1585+
15791586
def contour_dat():
15801587
x = np.linspace(-3, 5, 150)
15811588
y = np.linspace(-3, 5, 120)

0 commit comments

Comments
 (0)