While going through and updating some scripts to use the new features
that were recently added to hist(), I found myself very confused by
the align keywords - I had to go and look at Manuel Metz's post a
couple weeks ago to believe it wasn't a typo in the documentation...
"center" and "edge" are exactly the opposite of what one would have
thought (as he noted)... I've attached a diff of my proposed solution
- accepting the old keywords for backwards-compatibility, but have the
documentation only mention two keywords that make more sense ('left'
and 'mid').
I've added two other features as well - for some of the histograms I'm
making, it makes sense to have plots that are cumulative from the left
instead of the right - with this patch, that's allowed by passing in
cumulative=-1 (or anything else that is less than 0 - True still
operates the way it did before). To make this also easier from the
perspective of how some might want the histogram to look, I've also
added a 'right' option to the 'align' keyword.
Hopefully these changes will now satisfy all possible uses that anyone
can imagine for a histogram. :)
Index: lib/matplotlib/axes.py
===================================================================
--- lib/matplotlib/axes.py (revision 5368)
+++ lib/matplotlib/axes.py (working copy)
@@ -5592,7 +5592,7 @@
def hist(self, x, bins=10, normed=False, cumulative=False,
- bottom=None, histtype='bar', align='edge',
+ bottom=None, histtype='bar', align='mid',
orientation='vertical', rwidth=None, log=False, **kwargs):
"""
call signature::
@@ -5628,6 +5628,10 @@
The last bin gives the total number of datapoints. If normed is
also True then the histogram is normalized such that the last bin
equals one.
+ If a value that evaluates to less that one (e.g. -1), the
+ direction of accumulation is reversed. In this case, If normed is
+ also True then the histogram is normalized such that the first bin
+ equals one.
histtype:
[ 'bar' | 'barstacked' | 'step' ] The type of histogram
@@ -5636,9 +5640,10 @@
stacked on top of each other, and 'step' generates a lineplot.
align:
- ['edge' | 'center' ] Controles how the histogram is plotted.
- If 'edge', bars are centered between the bin edges.
- If 'center', bars are centered on the left bin edges
+ ['left' | 'mid' | 'right' ] Controles how the histogram is plotted.
+ If 'left', bars are centered on the left bin edges.
+ If 'mid', bars are centered between the bin edges.
+ If 'right', bars are centered on the right bin edges.
orientation:
[ 'horizontal' | 'vertical' ] If horizontal, barh will be used
@@ -5688,10 +5693,15 @@
n = [n,]
if cumulative:
- if normed:
- n = [(m * np.diff(bins)).cumsum() for m in n]
- else:
- n = [m.cumsum() for m in n]
+ if cumulative < 0:
+ slc=slice(None,None,-1)
+ else:
+ slc=slice(None)
+
+ if normed:
+ n = [(m * np.diff(bins))[slc].cumsum()[slc] for m in n]
+ else:
+ n = [m[slc].cumsum()[slc] for m in n]
ccount = 0
colors = _process_plot_var_args.defaultColors[:]
@@ -5721,9 +5731,13 @@
else:
raise ValueError, 'invalid histtype: %s' % histtype
- if align=='edge':
+ if align == 'mid' or align=='edge':
boffset += 0.5*totwidth
- elif align != 'center':
+ elif align == 'left' or align == 'center':
+ pass #TODO?
+ elif align == 'right':
+ boffset += totwidth
+ else:
raise ValueError, 'invalid align: %s' % align
if orientation == 'horizontal':
@@ -5758,10 +5772,14 @@
y = np.zeros( 2*len(bins), np.float_ )
x[0::2], x[1::2] = bins, bins
-
- if align == 'center':
+
+ if align == 'mid' or align=='edge':
+ pass
+ elif align == 'left' or align == 'center':
x -= 0.5*(bins[1]-bins[0])
- elif align != 'edge':
+ elif align == 'right':
+ x -= (bins[1]-bins[0])
+ else:
raise ValueError, 'invalid align: %s' % align
if log:
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel