8000 Change hist behavior when normed and stacked to something more sensible by neggert · Pull Request #1833 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Change hist behavior when normed and stacked to something more sensible #1833

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

Merged
merged 4 commits into from
May 9, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Nex 8000 t Next commit
Histograms with stacked=True, normed=True now normalize sum of histog…
…rams rather than each histogram individually.
  • Loading branch information
neggert committed May 8, 2013
commit 6917f4fab3916e30bf83adcd2925c907d44747c0
10 changes: 8 additions & 2 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8088,7 +8088,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
If `True`, the first element of the return tuple will
be the counts normalized to form a probability density, i.e.,
``n/(len(x)`dbin)``, ie the integral of the histogram will sum to
1.
1. If *stacked* is also *True*, the sum of the histograms is
normalized to 1.

weights : array_like, shape (n, ), optional, default: None
An array of weights, of the same shape as `x`. Each value in `x`
Expand Down Expand Up @@ -8300,16 +8301,21 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
# 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:
if normed and not stacked:
db = np.diff(bins)
m = (m.astype(float) / db) / m.sum()
if stacked:
m += mlast
mlast[:] = m
n.append(m)

if stacked and normed:
db = np.diff(bins)
for m in n:
m[:] = (m.astype(float) / db) / n[-1].sum()
if cumulative:
slc = slice(None)
if cbook.is_numlike(cumulative) and cumulative < 0:
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,15 @@ def test_hist_stacked_step():
ax = fig.add_subplot(111)
ax.hist( (d1, d2), histtype="step", stacked=True)

Copy link
Member

Choose a reason for hiding this comment

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

PEP8 : can you add another blank line before and after the new test? There should be two blank lines between functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nelle,

I fixed this instance, but that mistake occurs for almost every test in that file.

@image_comparison(baseline_images=['hist_stacked_normed'])
def test_hist_stacked_normed():
# make some data
d1 = np.linspace(1, 3, 20)
d2 = np.linspace(0, 10, 50)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist( (d1, d2), stacked=True, normed=True)
Copy link
Member

Choose a reason for hiding this comment

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

PEP 5A93 8 : there should be no space inbetween the two parenthesis.


@image_comparison(baseline_images=['hist_stacked_bar'])
def test_hist_stacked_bar():
# make some data
Expand Down
0