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

Skip to content

Commit 00cb616

Browse files
committed
Correctly convert units for a stacked histogram
1 parent 2ed9013 commit 00cb616

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 12 additions & 4 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,9 +6091,17 @@ 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)
6094+
# Process unit information
6095+
# If doing a stacked histogram, the input is a list of datasets, so
6096+
# we need to do the unit conversion individually on eaach dataset
6097+
if stacked:
6098+
self._process_unit_info(xdata=x[0], kwargs=kwargs)
6099+
for i, xi in enumerate(x):
6100+
x[i] = self.convert_xunits(xi)
6101+
else:
6102+
self._process_unit_info(xdata=x, kwargs=kwargs)
6103+
x = self.convert_xunits(x)
6104+
60976105
if bin_range is not None:
60986106
bin_range = self.convert_xunits(bin_range)
60996107

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)