Skip to content

Commit b4ed0f9

Browse files
committed
Merge pull request matplotlib#2409 from neggert/hist_bottom_fix2
Fix bugs related to bottom kwarg in step histograms
2 parents 66647b0 + ebf8853 commit b4ed0f9

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

lib/matplotlib/axes.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -8320,18 +8320,18 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
83208320
hist_kwargs = dict(range=bin_range)
83218321

83228322
n = []
8323-
mlast = bottom
8323+
mlast = None
83248324
for i in xrange(nx):
83258325
# this will automatically overwrite bins,
83268326
# so that each histogram uses the same bins
83278327
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
83288328
m = m.astype(float) # causes problems later if it's an int
8329-
if mlast is None:
8330-
mlast = np.zeros(len(bins)-1, m.dtype)
83318329
if normed and not stacked:
83328330
db = np.diff(bins)
83338331
m = (m.astype(float) / db) / m.sum()
83348332
if stacked:
8333+
if mlast is None:
8334+
mlast = np.zeros(len(bins)-1, m.dtype)
83358335
m += mlast
83368336
mlast[:] = m
83378337
n.append(m)
@@ -8422,6 +8422,12 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
84228422
x[0:2*len(bins)-1:2], x[1:2*len(bins)-1:2] = bins, bins[:-1]
84238423
x[2*len(bins)-1:] = x[1:2*len(bins)-1][::-1]
84248424

8425+
if bottom is None:
8426+
bottom = np.zeros(len(bins)-1, np.float)
8427+
8428+
y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = bottom, bottom
8429+
y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1]
8430+
84258431
if log:
84268432
if orientation == 'horizontal':
84278433
self.set_xscale('log', nonposx='clip')
@@ -8457,12 +8463,13 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
84578463

84588464
xvals, yvals = [], []
84598465
for m in n:
8460-
# starting point for drawing polygon
8461-
y[0] = y[1]
8462-
# top of the previous polygon becomes the bottom
8463-
y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1]
8466+
if stacked:
8467+
# starting point for drawing polygon
8468+
y[0] = y[1]
8469+
# top of the previous polygon becomes the bottom
8470+
y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1]
84648471
# set the top of this polygon
8465-
y[1:2*len(bins)-1:2], y[2:2*len(bins)-1:2] = m, m
8472+
y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = m + bottom, m + bottom
84668473
if log:
84678474
y[y < minimum] = minimum
84688475
if orientation == 'horizontal':
Loading

lib/matplotlib/tests/test_axes.py

+9
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,15 @@ def test_hist_stacked_normed():
11421142
ax.hist((d1, d2), stacked=True, normed=True)
11431143

11441144

1145+
@image_comparison(baseline_images=['hist_step_bottom'], extensions=['png'], remove_text=True)
1146+
def test_hist_step_bottom():
1147+
# make some data
1148+
d1 = np.linspace(1, 3, 20)
1149+
fig = plt.figure()
1150+
ax = fig.add_subplot(111)
1151+
ax.hist(d1, bottom=np.arange(10), histtype="stepfilled")
1152+
1153+
11451154
@image_comparison(baseline_images=['hist_stacked_bar'])
11461155
def test_hist_stacked_bar():
11471156
# make some data

0 commit comments

Comments
 (0)