8000 Merge pull request #9654 from dstansby/stacked-datetime · matplotlib/matplotlib@905465b · GitHub
[go: up one dir, main page]

Skip to content

Commit 905465b

Browse files
authored
Merge pull request #9654 from dstansby/stacked-datetime
Correctly convert units for a stacked histogram
2 parents c508c35 + 7fed800 commit 905465b

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5892,7 +5892,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
58925892
Parameters
58935893
----------
58945894
x : (n,) array or sequence of (n,) arrays
5895-
Input values, this takes either a single array or a sequency of
5895+
Input values, this takes either a single array or a sequence of
58965896
arrays which are not required to be of the same length
58975897
58985898
bins : integer or sequence or 'auto', optional
@@ -6104,30 +6104,31 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
61046104
"Please only use 'density', since 'normed'"
61056105
"will be deprecated.")
61066106

6107-
# process the unit information
6108-
self._process_unit_info(xdata=x, kwargs=kwargs)
6109-
x = self.convert_xunits(x)
6110-
if bin_range is not None:
6111-
bin_range = self.convert_xunits(bin_range)
6112-
6113-
# Check whether bins or range are given explicitly.
6114-
binsgiven = (cbook.iterable(bins) or bin_range is not None)
6115-
61166107
# basic input validation
61176108
input_empty = np.size(x) == 0
6118-
61196109
# Massage 'x' for processing.
61206110
if input_empty:
6121-
x = np.array([[]])
6111+
x = [np.array([])]
61226112
else:
61236113
x = cbook._reshape_2D(x, 'x')
61246114
nx = len(x) # number of datasets
61256115

6116+
# Process unit information
6117+
# Unit conversion is done individually on each dataset
6118+
self._process_unit_info(xdata=x[0], kwargs=kwargs)
6119+
x = [self.convert_xunits(xi) for xi in x]
6120+
6121+
if bin_range is not None:
6122+
bin_range = self.convert_xunits(bin_range)
6123+
6124+
# Check whether bins or range are given explicitly.
6125+
binsgiven = (cbook.iterable(bins) or bin_range is not None)
6126+
61266127
# We need to do to 'weights' what was done to 'x'
61276128
if weights is not None:
61286129
w = cbook._reshape_2D(weights, 'weights')
61296130
else:
6130-
w = [None]*nx
6131+
w = [None] * nx
61316132

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

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,14 @@ def test_hist_unequal_bins_density():
15921592
assert_allclose(mpl_heights, np_heights)
15931593

15941594

1595+
def test_hist_datetime_datasets():
1596+
data = [[datetime.datetime(2017, 1, 1), datetime.datetime(2017, 1, 1)],
1597+
[datetime.datetime(2017, 1, 1), datetime.datetime(2017, 1, 2)]]
1598+
fig, ax = plt.subplots()
1599+
ax.hist(data, stacked=True)
1600+
ax.hist(data, stacked=False)
1601+
1602+
15951603
def contour_dat():
15961604
x = np.linspace(-3, 5, 150)
15971605
y = np.linspace(-3, 5, 120)

0 commit comments

Comments
 (0)
0