Skip to content

Fix bugs in stacked histograms #1706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4450,7 +4450,7 @@ def legend(self, *args, **kwargs):

*framealpha*: [*None* | float]
If not None, alpha channel for legend frame. Default *None*.

*ncol* : integer
number of columns. default is 1

Expand Down Expand Up @@ -8076,10 +8076,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,

n = []
mlast = bottom
# reversed order is necessary so when stacking histogram, first
# dataset is on top if histogram isn't stacked, this doesn't make any
# difference
for i in reversed(xrange(nx)):
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)
Expand All @@ -8103,8 +8100,6 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
else:
n = [m[slc].cumsum()[slc] for m in n]

n.reverse() # put them back in the right order

patches = []

if histtype.startswith('bar'):
Expand Down Expand Up @@ -8145,7 +8140,9 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
patch = _barfunc(bins[:-1] + boffset, m, width,
align='center', log=log,
color=c)
patches.append(patch)
# need to turn this into a collection so that it's consistent
# with the step histogram
patches.append(mcoll.PatchCollection(patch))
boffset += dw

elif histtype.startswith('step'):
Expand All @@ -8172,19 +8169,22 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
# overriding this
fill = (histtype == 'stepfilled')

y_bottom = y[:]
for m, c in zip(n, color):
if stacked:
y_bottom = y[:]
y[1:-1:2], y[2::2] = m, m
if log:
y[y < minimum] = minimum
if orientation == 'horizontal':
x, y = y, x

if fill:
patches.append(self.fill(x, y,
closed=False, facecolor=c))
patches.append(self.fill_between(x, y, y_bottom,
closed=False, facecolors=c))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 nitpick: this line is under-indented: it should be align with the opening ( of the previous line

else:
patches.append(self.fill(x, y,
closed=False, edgecolor=c, fill=False))
patches.append(self.fill_between(x, y, y_bottom,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

closed=False, edgecolors=c, fill=False))

# adopted from adjust_x/ylim part of the bar method
if orientation == 'horizontal':
Expand Down Expand Up @@ -8218,17 +8218,11 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
labels += [None] * (nx - len(labels))

for (patch, lbl) in zip(patches, labels):
if patch:
p = patch[0]
p.update(kwargs)
if lbl is not None:
p.set_label(lbl)

p.set_snap(False)
patch.update(kwargs)
if lbl is not None:
patch.set_label(lbl)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the switch to fill_between, we're dealing with a PolyCollection rather than a list of patches. Thus, some changes here. I also put the patches from the bar histtype into a PatchCollection, so they can be treated using the same interface.


for p in patch[1:]:
p.update(kwargs)
p.set_label('_nolegend_')
patch.set_snap(False)

if binsgiven:
if orientation == 'vertical':
Expand Down