Histogram align 'edge' or 'center' bug?

David Fokkema <dfokkema@...1511...> writes:

If I choose center, the result is that my histogram is calculated
for edge values but the bars are placed at center values which is
completely misleading and wrong! I'd say this is a bug, but I may be
overlooking something here...

Looks like a bug to me. Could you file it at

so it isn't forgotten?

···

--
Jouni K. Sepp�nen

Well... It couldn't be too hard to fix, I guess... I know python, I
tracked down the source, I could try and fix it, right? I think I'll
have the time next Tuesday, so hopefully I'll file a bug report with an
attached patch, :wink:

···

On Fri, 2007-04-06 at 18:32 +0300, Jouni K. Seppänen wrote:

David Fokkema <dfokkema@...1511...> writes:

> If I choose center, the result is that my histogram is calculated
> for edge values but the bars are placed at center values which is
> completely misleading and wrong! I'd say this is a bug, but I may be
> overlooking something here...

Looks like a bug to me. Could you file it at
matplotlib download | SourceForge.net
so it isn't forgotten?

I fixed the bug, I think. At least it's working on my system and I think
it is not invasive. Comments please? I'll send it upstream otherwise...

--- matplotlib/axes.py.orig 2007-04-10 10:58:30.000000000 +0200
+++ matplotlib/axes.py 2007-04-10 11:14:56.000000000 +0200
@@ -4149,7 +4149,7 @@
         hist bars
         """
         if not self._hold: self.cla()
- n, bins = matplotlib.mlab.hist(x, bins, normed)
+ n, bins = matplotlib.mlab.hist(x, bins, normed, align)
         if width is None: width = 0.9*(bins[1]-bins[0])
         if orientation == 'horizontal':
             patches = self.barh(bins, n, height=width, left=bottom,
align=align)
--- matplotlib/mlab.py.orig 2007-04-10 11:16:23.000000000 +0200
+++ matplotlib/mlab.py 2007-04-10 11:24:48.000000000 +0200
@@ -597,7 +597,7 @@
    #S = -1.0*asum(p*log(p))
    return S

-def hist(y, bins=10, normed=0):
+def hist(y, bins=10, normed=0, align='edge'):
     """
     Return the histogram of y with bins equally sized bins. If bins
     is an array, use the bins. Return value is
@@ -626,11 +626,16 @@
         dy = (ymax-ymin)/bins
         bins = ymin + dy*arange(bins)

+ if align == 'center':
+ hw = .5*(bins[1]-bins[0])
+ nbins = [x-hw for x in bins]
+ else:
+ nbins = bins

- n = searchsorted(sort(y), bins)
+ n = searchsorted(sort(y), nbins)
     n = diff(concatenate([n, [len(y)]]))
     if normed:
- db = bins[1]-bins[0]
+ db = nbins[1]-nbins[0]
        return 1/(len(y)*db)*n, bins
     else:
        return n, bins

Thanks,

David

···

On Sun, 2007-04-08 at 19:25 +0200, David Fokkema wrote:

On Fri, 2007-04-06 at 18:32 +0300, Jouni K. Seppänen wrote:
> David Fokkema <dfokkema@...1511...> writes:
>
> > If I choose center, the result is that my histogram is calculated
> > for edge values but the bars are placed at center values which is
> > completely misleading and wrong! I'd say this is a bug, but I may be
> > overlooking something here...
>
> Looks like a bug to me. Could you file it at
> matplotlib download | SourceForge.net
> so it isn't forgotten?

Well... It couldn't be too hard to fix, I guess... I know python, I
tracked down the source, I could try and fix it, right? I think I'll
have the time next Tuesday, so hopefully I'll file a bug report with an
attached patch, :wink: