diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index a3a92ee54d1f..ee0ffbfa2fbd 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8319,18 +8319,18 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, hist_kwargs = dict(range=bin_range) n = [] - mlast = bottom + mlast = None for i in xrange(nx): # this will automatically overwrite bins, # so that each histogram uses the same bins m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs) m = m.astype(float) # causes problems later if it's an int - if mlast is None: - mlast = np.zeros(len(bins)-1, m.dtype) if normed and not stacked: db = np.diff(bins) m = (m.astype(float) / db) / m.sum() if stacked: + if mlast is None: + mlast = np.zeros(len(bins)-1, m.dtype) m += mlast mlast[:] = m n.append(m) @@ -8421,6 +8421,12 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, x[0:2*len(bins)-1:2], x[1:2*len(bins)-1:2] = bins, bins[:-1] x[2*len(bins)-1:] = x[1:2*len(bins)-1][::-1] + if bottom is None: + bottom = np.zeros(len(bins)-1, np.float) + + y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = bottom, bottom + y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1] + if log: if orientation == 'horizontal': self.set_xscale('log', nonposx='clip') @@ -8456,12 +8462,13 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, xvals, yvals = [], [] for m in n: - # starting point for drawing polygon - y[0] = y[1] - # top of the previous polygon becomes the bottom - y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1] + if stacked: + # starting point for drawing polygon + y[0] = y[1] + # top of the previous polygon becomes the bottom + y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1] # set the top of this polygon - y[1:2*len(bins)-1:2], y[2:2*len(bins)-1:2] = m, m + y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = m + bottom, m + bottom if log: y[y < minimum] = minimum if orientation == 'horizontal': diff --git a/lib/matplotlib/tests/baseline_images/test_axes/hist_step_bottom.png b/lib/matplotlib/tests/baseline_images/test_axes/hist_step_bottom.png new file mode 100644 index 000000000000..164ec8cb04db Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/hist_step_bottom.png differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 7211995ed46a..a064926cee85 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1127,6 +1127,15 @@ def test_hist_stacked_normed(): ax.hist((d1, d2), stacked=True, normed=True) +@image_comparison(baseline_images=['hist_step_bottom'], extensions=['png'], remove_text=True) +def test_hist_step_bottom(): + # make some data + d1 = np.linspace(1, 3, 20) + fig = plt.figure() + ax = fig.add_subplot(111) + ax.hist(d1, bottom=np.arange(10), histtype="stepfilled") + + @image_comparison(baseline_images=['hist_stacked_bar']) def test_hist_stacked_bar(): # make some data