--- C:\home\mspacek\Desktop\axes.svn2495.py 2006-06-20 16:56:06.000000000 -0700 +++ C:\home\mspacek\Desktop\axes.patch4.py 2006-06-22 03:41:56.000000000 -0700 @@ -2090,13 +2090,13 @@ autoscaled; default True. See Axes.autoscale_view for more information """ d = kwargs.copy() scalex = d.pop('scalex', True) - scaley = d.pop('scaley', True) + scaley = d.pop('scaley', True) if not self._hold: self.cla() lines = [] for line in self._get_lines(*args, **d): self.add_line(line) lines.append(line) lines = [line for line in lines] # consume the generator @@ -2361,185 +2361,219 @@ #### Specialized plotting def bar(self, left, height, width=0.8, bottom=0, - color='b', yerr=None, xerr=None, ecolor='k', capsize=3 + color=None, edgecolor=None, yerr=None, xerr=None, ecolor=None, capsize=3, + horizontal=False ): """ BAR(left, height, width=0.8, bottom=0, - color='b', yerr=None, xerr=None, ecolor='k', capsize=3) + color=None, edgecolor=None, yerr=None, xerr=None, ecolor=None, capsize=3, + horizontal=False) - Make a bar plot with rectangles at + Make a bar plot with rectangles bounded by - left, left+width, 0, height + left, left+width, bottom, bottom+height (left, right, bottom and top edges) - left and height are Numeric arrays. + left, height, width, and bottom can be either scalars or sequences Return value is a list of Rectangle patch instances BAR(left, height, width, bottom, - color, yerr, xerr, capsize, yoff) + color, edgecolor, yerr, xerr, ecolor, capsize) + left - the x coordinates of the left sides of the bars + + height - the heights of the bars + + Optional arguments + + width - the widths of the bars + + bottom - the y coordinates of the bottom edges of the bars + + color specifies the colors of the bars + + edgecolor specifies the colors of the bar edges + xerr and yerr, if not None, will be used to generate errorbars - on the bar chart + on the bar chart - color specifies the color of the bar - ecolor specifies the color of any errorbar capsize determines the length in points of the error bar caps + horizontal is a boolean that controls whether to interpret the data for + plotting horizontal bars or vertical ones (the default) - The optional arguments color, width and bottom can be either - scalars or len(x) sequences + The optional arguments color, edgecolor, yerr, and xerr can be either + scalars or sequences of the appropriate length This enables you to use bar as the basis for stacked bar charts, or candlestick plots """ if not self._hold: self.cla() - # left = asarray(left) - width/2 + def make_iterable(x): + if not iterable(x): + return [x] + else: + return x + + # make them safe to take len() of + left = make_iterable(left) + height = make_iterable(height) + width = make_iterable(width) + bottom = make_iterable(bottom) + + if not horizontal: + # resize width and bottom according to length of left + nbars = len(left) + if len(width) == 1: + width *= nbars + if len(bottom) == 1: + bottom *= nbars + else: + # resize left and height according to length of bottom + nbars = len(bottom) + if len(left) == 1: + left *= nbars + if len(height) == 1: + height *= nbars + left = asarray(left) height = asarray(height) - - patches = [] - + width = asarray(width) + bottom = asarray(bottom) # if color looks like a color string, an RGB tuple or a - # scalar, then repeat it by len(x) + # scalar, then repeat it by nbars if (is_string_like(color) or - (iterable(color) and len(color)==3 and len(left)!=3) or + (iterable(color) and len(color)==3 and nbars!=3) or not iterable(color)): - color = [color]*len(left) + color = [color]*nbars + # if edgecolor looks like a color string, an RGB tuple or a + # scalar, then repeat it by nbars + if (is_string_like(edgecolor) or + (iterable(edgecolor) and len(edgecolor)==3 and nbars!=3) or + not iterable(edgecolor)): + edgecolor = [edgecolor]*nbars - if not iterable(bottom): - bottom = array([bottom]*len(left), Float) + if not iterable(yerr): + yerr = asarray([yerr]*nbars, Float) # Float converts Nones to NANs else: - bottom = asarray(bottom) - if not iterable(width): - width = array([width]*len(left), Float) + yerr = asarray(yerr) + if not iterable(xerr): + xerr = asarray([xerr]*nbars, Float) else: - width = asarray(width) + xerr = asarray(xerr) - N = len(left) - assert len(bottom)==N, 'bar arg bottom must be len(left)' - assert len(width)==N, 'bar arg width must be len(left) or scalar' - assert len(height)==N, 'bar arg height must be len(left) or scalar' - assert len(color)==N, 'bar arg color must be len(left) or scalar' + if not horizontal: + func = 'bar' + lenarg = 'left' + else: + func = 'barh' + lenarg = 'bottom' + assert len(left)==nbars, '%s argument \'left\' must be len(%s) or scalar' % (func, lenarg) + assert len(height)==nbars, '%s argument \'height\' must be len(%s) or scalar' % (func, lenarg) + assert len(width)==nbars, '%s argument \'width\' must be len(%s) or scalar' % (func, lenarg) + assert len(bottom)==nbars, '%s argument \'bottom\' must be len(%s) or scalar' % (func, lenarg) + assert len(color)==nbars, '%s argument \'color\' must be len(%s) or scalar' % (func, lenarg) + assert len(edgecolor)==nbars, '%s argument \'edgecolor\' must be len(%s) or scalar' % (func, lenarg) + assert len(yerr)==nbars, '%s argument \'yerr\' must be len(%s) or scalar' % (func, lenarg) + assert len(xerr)==nbars, '%s argument \'xerr\' must be len(%s) or scalar' % (func, lenarg) - args = zip(left, bottom, width, height, color) - for l, b, w, h, c in args: + patches = [] + + args = zip(left, bottom, width, height, color, edgecolor) + for l, b, w, h, c, e in args: if h<0: b += h h = abs(h) r = Rectangle( xy=(l, b), width=w, height=h, facecolor=c, + edgecolor=e, ) self.add_patch(r) patches.append(r) + holdstate = self._hold + self.hold(True) # ensure hold is on before plotting errorbars if xerr is not None or yerr is not None: + if not horizontal: + x, y = left+0.5*width, bottom+height + else: + x, y = left+width, bottom+0.5*height self.errorbar( - left+0.5*width, bottom+height, + x, y, yerr=yerr, xerr=xerr, fmt=None, ecolor=ecolor, capsize=capsize) + + self.hold(holdstate) # restore previous hold state + self.autoscale_view() return patches - def barh(self, x, y, height=0.8, left=0, - color='b', yerr=None, xerr=None, ecolor='k', capsize=3 - ): + + def barh(self, bottom, width, height=0.8, left=0, + color=None, edgecolor=None, xerr=None, yerr=None, ecolor=None, capsize=3 + ): """ - BARH(x, y, height=0.8, left=0, - color='b', yerr=None, xerr=None, ecolor='k', capsize=3) + BARH(bottom, width, height=0.8, left=0, + color=None, edgecolor=None, xerr=None, yerr=None, ecolor=None, capsize=3) - BARH(x, y) + Make a horizontal bar plot with rectangles bounded by - The y values give the heights of the center of the bars. The - x values give the length of the bars. + left, left+width, bottom, bottom+height (left, right, bottom and top edges) + + bottom, width, height, and left can be either scalars or sequences - Return value is a list of Rectangle patch instances + Return value is a list of Rectangle patch instances + BARH(bottom, width, height, left, + color, edgecolor, xerr, yerr, ecolor, capsize) + + bottom - the vertical positions of the bottom edges of the bars + + width - the lengths of the bars + Optional arguments - height - the height (thickness) of the bar + height - the heights (thicknesses) of the bars - left - the x coordinate of the left side of the bar + left - the x coordinates of the left edges of the bars - color specifies the color of the bar + color specifies the colors of the bars + edgecolor specifies the colors of the bar edges + xerr and yerr, if not None, will be used to generate errorbars on the bar chart ecolor specifies the color of any errorbar capsize determines the length in points of the error bar caps - The optional arguments color, height and left can be either - scalars or len(x) sequences + The optional arguments color, edgecolor, xerr, and yerr can be either + scalars or sequences of the appropriate length + + This enables you to use barh as the basis for stacked bar + charts, or candlestick plots """ - if not self._hold: self.cla() - # left = asarray(left) - width/2 - x = asarray(x) - y = asarray(y) - - patches = [] - - - # if color looks like a color string, and RGB tuple or a - # scalar, then repeat it by len(x) - if (is_string_like(color) or - (iterable(color) and len(color)==3 and - iterable(left) and len(left)!=3) or not iterable(color)): - color = [color]*len(x) - - - if not iterable(left): - left = array([left]*len(x), Float) - else: - left = asarray(left) - if not iterable(height): - height = array([height]*len(x), Float) - else: - height = asarray(height) - - N = len(x) - assert len(left)==N, 'bar arg left must be len(x)' - assert len(height)==N, 'bar arg height must be len(x) or scalar' - assert len(y)==N, 'bar arg y must be len(x) or scalar' - assert len(color)==N, 'bar arg color must be len(x) or scalar' - - width = x - right = left+x - bottom = y - height/2. - - args = zip(left, bottom, width, height, color) - for l, b, w, h, c in args: - if h<0: - b += h - h = abs(h) - r = Rectangle( - xy=(l, b), width=w, height=h, - facecolor=c, - ) - self.add_patch(r) - patches.append(r) - - if xerr is not None or yerr is not None: - self.errorbar( - right, y, - yerr=yerr, xerr=xerr, - fmt=None, ecolor=ecolor, capsize=capsize) - self.autoscale_view() + patches = self.bar(left=left, height=height, width=width, bottom=bottom, + color=color, edgecolor=edgecolor, yerr=yerr, xerr=xerr, ecolor=ecolor, capsize=capsize, + horizontal=True + ) return patches + def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-'): """ STEM(x, y, linefmt='b-', markerfmt='bo', basefmt='r-') A stem plot plots vertical lines (using linefmt) at each x location @@ -3994,13 +4028,13 @@ hist bars """ if not self._hold: self.cla() n,bins = matplotlib.mlab.hist(x, bins, normed) if width is None: width = 0.9*(bins[1]-bins[0]) if orientation=='horizontal': - patches = self.barh(n, bins, height=width, left=bottom) + patches = self.barh(bins, n, height=width, left=bottom) else: patches = self.bar(bins, n, width=width, bottom=bottom) for p in patches: p.update(kwargs) return n, bins, silent_list('Patch', patches)