@@ -2364,180 +2364,238 @@ def get_handles():
23642364
23652365
23662366 def bar (self , left , height , width = 0.8 , bottom = 0 ,
2367- color = 'b' , yerr = None , xerr = None , ecolor = 'k' , capsize = 3
2367+ color = None , edgecolor = None , yerr = None , xerr = None , ecolor = None , capsize = 3 ,
2368+ align = 'edge' , orientation = 'vertical'
23682369 ):
23692370 """
23702371 BAR(left, height, width=0.8, bottom=0,
2371- color='b', yerr=None, xerr=None, ecolor='k', capsize=3)
2372+ color=None, edgecolor=None, yerr=None, xerr=None, ecolor=None, capsize=3,
2373+ align='edge', orientation='vertical')
23722374
2373- Make a bar plot with rectangles at
2375+ Make a bar plot with rectangles bounded by
23742376
2375- left, left+width, 0, height
2377+ left, left+width, bottom, bottom+ height (left, right, bottom and top edges)
23762378
2377- left and height are Numeric arrays.
2379+ left, height, width, and bottom can be either scalars or sequences
23782380
23792381 Return value is a list of Rectangle patch instances
23802382
23812383 BAR(left, height, width, bottom,
2382- color, yerr, xerr, capsize, yoff)
2384+ color, edgecolor, yerr, xerr, ecolor, capsize,
2385+ align, orientation)
23832386
2384- xerr and yerr, if not None, will be used to generate errorbars
2385- on the bar chart
2387+ left - the x coordinates of the left sides of the bars
2388+
2389+ height - the heights of the bars
2390+
2391+ Optional arguments
2392+
2393+ width - the widths of the bars
23862394
2387- color specifies the color of the bar
2395+ bottom - the y coordinates of the bottom edges of the bars
2396+
2397+ color specifies the colors of the bars
2398+
2399+ edgecolor specifies the colors of the bar edges
2400+
2401+ xerr and yerr, if not None, will be used to generate errorbars
2402+ on the bar chart
23882403
23892404 ecolor specifies the color of any errorbar
23902405
23912406 capsize determines the length in points of the error bar caps
23922407
2408+ align = 'edge' | 'center'
2409+
2410+ orientation = 'vertical' | 'horizontal'
23932411
2394- The optional arguments color, width and bottom can be either
2395- scalars or len(x) sequences
2412+ For vertical bars, 'edge' aligns bars by their left edges in left,
2413+ while 'center' interprets these values as the x coordinates of the bar centers.
2414+ For horizontal bars, 'edge' aligns bars by their bottom edges in bottom,
2415+ while 'center' interprets these values as the y coordinates of the bar centers.
2416+
2417+ The optional arguments color, edgecolor, yerr, and xerr can be either
2418+ scalars or sequences of length equal to the number of bars
23962419
23972420 This enables you to use bar as the basis for stacked bar
23982421 charts, or candlestick plots
23992422 """
24002423 if not self ._hold : self .cla ()
24012424
2402- # left = asarray(left) - width/2
2425+ def make_iterable (x ):
2426+ if not iterable (x ):
2427+ return [x ]
2428+ else :
2429+ return x
2430+
2431+ # make them safe to take len() of
2432+ left = make_iterable (left )
2433+ height = make_iterable (height )
2434+ width = make_iterable (width )
2435+ bottom = make_iterable (bottom )
2436+
2437+ if orientation == 'vertical' :
2438+ # size width and bottom according to length of left
2439+ nbars = len (left )
2440+ if len (width ) == 1 :
2441+ width *= nbars
2442+ if len (bottom ) == 1 :
2443+ bottom *= nbars
2444+ elif orientation == 'horizontal' :
2445+ # size left and height according to length of bottom
2446+ nbars = len (bottom )
2447+ if len (left ) == 1 :
2448+ left *= nbars
2449+ if len (height ) == 1 :
2450+ height *= nbars
2451+ else :
2452+ raise ValueError , 'invalid orientation: %s' % orientation
2453+
24032454 left = asarray (left )
24042455 height = asarray (height )
2405-
2406- patches = []
2407-
2456+ width = asarray (width )
2457+ bottom = asarray (bottom )
24082458
24092459 # if color looks like a color string, an RGB tuple or a
2410- # scalar, then repeat it by len(x)
2460+ # scalar, then repeat it by nbars
24112461 if (is_string_like (color ) or
2412- (iterable (color ) and len (color )== 3 and len ( left ) != 3 ) or
2462+ (iterable (color ) and len (color )== 3 and nbars != 3 ) or
24132463 not iterable (color )):
2414- color = [color ]* len ( left )
2464+ color = [color ]* nbars
24152465
2466+ # if edgecolor looks like a color string, an RGB tuple or a
2467+ # scalar, then repeat it by nbars
2468+ if (is_string_like (edgecolor ) or
2469+ (iterable (edgecolor ) and len (edgecolor )== 3 and nbars != 3 ) or
2470+ not iterable (edgecolor )):
2471+ edgecolor = [edgecolor ]* nbars
24162472
2417- if not iterable (bottom ):
2418- bottom = array ([ bottom ] * len ( left ) , Float )
2473+ if not iterable (yerr ):
2474+ yerr = asarray ([ yerr ] * nbars , Float ) # Float converts Nones to NANs
24192475 else :
2420- bottom = asarray (bottom )
2421- if not iterable (width ):
2422- width = array ([ width ] * len ( left ) , Float )
2476+ yerr = asarray (yerr )
2477+ if not iterable (xerr ):
2478+ xerr = asarray ([ xerr ] * nbars , Float )
24232479 else :
2424- width = asarray (width )
2480+ xerr = asarray (xerr )
2481+
2482+ if orientation == 'vertical' :
2483+ lenarg = 'left'
2484+ elif orientation == 'horizontal' :
2485+ lenarg = 'bottom'
2486+ assert len (left )== nbars , 'bar() argument \' left\' must be len(%s) or scalar' % lenarg
2487+ assert len (height )== nbars , 'bar() argument \' height\' must be len(%s) or scalar' % lenarg
2488+ assert len (width )== nbars , 'bar() argument \' width\' must be len(%s) or scalar' % lenarg
2489+ assert len (bottom )== nbars , 'bar() argument \' bottom\' must be len(%s) or scalar' % lenarg
2490+ assert len (color )== nbars , 'bar() argument \' color\' must be len(%s) or scalar' % lenarg
2491+ assert len (edgecolor )== nbars , 'bar() argument \' edgecolor\' must be len(%s) or scalar' % lenarg
2492+ assert len (yerr )== nbars , 'bar() argument \' yerr\' must be len(%s) or scalar' % lenarg
2493+ assert len (xerr )== nbars , 'bar() argument \' xerr\' must be len(%s) or scalar' % lenarg
2494+
2495+ patches = []
24252496
2426- N = len (left )
2427- assert len (bottom )== N , 'bar arg bottom must be len(left)'
2428- assert len (width )== N , 'bar arg width must be len(left) or scalar'
2429- assert len (height )== N , 'bar arg height must be len(left) or scalar'
2430- assert len (color )== N , 'bar arg color must be len(left) or scalar'
2497+ if align == 'edge' :
2498+ pass
2499+ elif align == 'center' :
2500+ if orientation == 'vertical' :
2501+ left = left - width / 2.
2502+ elif orientation == 'horizontal' :
2503+ bottom = bottom - height / 2.
2504+ else :
2505+ raise ValueError , 'invalid alignment: %s' % align
24312506
2432- args = zip (left , bottom , width , height , color )
2433- for l , b , w , h , c in args :
2507+ args = zip (left , bottom , width , height , color , edgecolor )
2508+ for l , b , w , h , c , e in args :
24342509 if h < 0 :
24352510 b += h
24362511 h = abs (h )
24372512 r = Rectangle (
24382513 xy = (l , b ), width = w , height = h ,
24392514 facecolor = c ,
2515+ edgecolor = e ,
24402516 )
24412517 self .add_patch (r )
24422518 patches .append (r )
24432519
2520+ holdstate = self ._hold
2521+ self .hold (True ) # ensure hold is on before plotting errorbars
24442522
24452523 if xerr is not None or yerr is not None :
2524+ if orientation == 'vertical' :
2525+ x , y = left + 0.5 * width , bottom + height
2526+ elif orientation == 'horizontal' :
2527+ x , y = left + width , bottom + 0.5 * height
24462528 self .errorbar (
2447- left + 0.5 * width , bottom + height ,
2529+ x , y ,
24482530 yerr = yerr , xerr = xerr ,
24492531 fmt = None , ecolor = ecolor , capsize = capsize )
2532+
2533+ self .hold (holdstate ) # restore previous hold state
2534+
24502535 self .autoscale_view ()
24512536 return patches
24522537
2453- def barh (self , x , y , height = 0.8 , left = 0 ,
2454- color = 'b' , yerr = None , xerr = None , ecolor = 'k' , capsize = 3
2455- ):
2456- """
2457- BARH(x, y, height=0.8, left=0,
2458- color='b', yerr=None, xerr=None, ecolor='k', capsize=3)
2459-
2460- BARH(x, y)
24612538
2462- The y values give the heights of the center of the bars. The
2463- x values give the length of the bars.
2539+ def barh (self , bottom , width , height = 0.8 , left = 0 ,
2540+ color = None , edgecolor = None , xerr = None , yerr = None , ecolor = None , capsize = 3 ,
2541+ align = 'edge'
2542+ ):
2543+ """
2544+ BARH(bottom, width, height=0.8, left=0,
2545+ color=None, edgecolor=None, xerr=None, yerr=None, ecolor=None, capsize=3,
2546+ align='edge')
24642547
2465- Return value is a list of Rectangle patch instances
2548+ Make a horizontal bar plot with rectangles bounded by
24662549
2467- Optional arguments
2550+ left, left+width, bottom, bottom+height (left, right, bottom and top edges)
24682551
2469- height - the height (thickness) of the bar
2552+ bottom, width, height, and left can be either scalars or sequences
24702553
2471- left - the x coordinate of the left side of the bar
2554+ Return value is a list of Rectangle patch instances
24722555
2473- color specifies the color of the bar
2556+ BARH(bottom, width, height, left,
2557+ color, edgecolor, xerr, yerr, ecolor, capsize,
2558+ align)
24742559
2475- xerr and yerr, if not None, will be used to generate errorbars
2476- on the bar chart
2560+ bottom - the vertical positions of the bottom edges of the bars
24772561
2478- ecolor specifies the color of any errorbar
2562+ width - the lengths of the bars
24792563
2480- capsize determines the length in points of the error bar caps
2564+ Optional arguments
24812565
2482- The optional arguments color, height and left can be either
2483- scalars or len(x) sequences
2484- """
2485- if not self ._hold : self .cla ()
2566+ height - the heights (thicknesses) of the bars
24862567
2487- # left = asarray(left) - width/2
2488- x = asarray (x )
2489- y = asarray (y )
2568+ left - the x coordinates of the left edges of the bars
24902569
2491- patches = []
2570+ color specifies the colors of the bars
24922571
2572+ edgecolor specifies the colors of the bar edges
24932573
2494- # if color looks like a color string, and RGB tuple or a
2495- # scalar, then repeat it by len(x)
2496- if (is_string_like (color ) or
2497- (iterable (color ) and len (color )== 3 and
2498- iterable (left ) and len (left )!= 3 ) or not iterable (color )):
2499- color = [color ]* len (x )
2574+ xerr and yerr, if not None, will be used to generate errorbars
2575+ on the bar chart
25002576
2577+ ecolor specifies the color of any errorbar
25012578
2502- if not iterable (left ):
2503- left = array ([left ]* len (x ), Float )
2504- else :
2505- left = asarray (left )
2506- if not iterable (height ):
2507- height = array ([height ]* len (x ), Float )
2508- else :
2509- height = asarray (height )
2579+ capsize determines the length in points of the error bar caps
25102580
2511- N = len (x )
2512- assert len (left )== N , 'bar arg left must be len(x)'
2513- assert len (height )== N , 'bar arg height must be len(x) or scalar'
2514- assert len (y )== N , 'bar arg y must be len(x) or scalar'
2515- assert len (color )== N , 'bar arg color must be len(x) or scalar'
2581+ align = 'edge' | 'center'
2582+ 'edge' aligns the horizontal bars by their bottom edges in bottom, while
2583+ 'center' interprets these values as the y coordinates of the bar centers.
25162584
2517- width = x
2518- right = left + x
2519- bottom = y - height / 2.
2585+ The optional arguments color, edgecolor, xerr, and yerr can be either
2586+ scalars or sequences of length equal to the number of bars
25202587
2521- args = zip (left , bottom , width , height , color )
2522- for l , b , w , h , c in args :
2523- if h < 0 :
2524- b += h
2525- h = abs (h )
2526- r = Rectangle (
2527- xy = (l , b ), width = w , height = h ,
2528- facecolor = c ,
2529- )
2530- self .add_patch (r )
2531- patches .append (r )
2588+ This enables you to use barh as the basis for stacked bar
2589+ charts, or candlestick plots
2590+ """
25322591
2533- if xerr is not None or yerr is not None :
2534- self .errorbar (
2535- right , y ,
2536- yerr = yerr , xerr = xerr ,
2537- fmt = None , ecolor = ecolor , capsize = capsize )
2538- self .autoscale_view ()
2592+ patches = self .bar (left = left , height = height , width = width , bottom = bottom ,
2593+ color = color , edgecolor = edgecolor , yerr = yerr , xerr = xerr , ecolor = ecolor , capsize = capsize ,
2594+ align = align , orientation = 'horizontal'
2595+ )
25392596 return patches
25402597
2598+
25412599 def stem (self , x , y , linefmt = 'b-' , markerfmt = 'bo' , basefmt = 'r-' ):
25422600 """
25432601 STEM(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')
@@ -3978,9 +4036,10 @@ def table(self, **kwargs):
39784036 #### Data analysis
39794037
39804038 def hist (self , x , bins = 10 , normed = 0 , bottom = 0 ,
3981- orientation = 'vertical' , width = None , ** kwargs ):
4039+ align = 'edge' , orientation = 'vertical' , width = None , ** kwargs ):
39824040 """
3983- HIST(x, bins=10, normed=0, bottom=0, orientiation='vertical', **kwargs)
4041+ HIST(x, bins=10, normed=0, bottom=0,
4042+ align='edge', orientation='vertical', width=None, **kwargs)
39844043
39854044 Compute the histogram of x. bins is either an integer number of
39864045 bins or a sequence giving the bins. x are the data to be binned.
@@ -3991,9 +4050,11 @@ def hist(self, x, bins=10, normed=0, bottom=0,
39914050 be the counts normalized to form a probability density, ie,
39924051 n/(len(x)*dbin)
39934052
4053+ align = 'edge' | 'center'. Interprets bins either as edge
4054+ or center values
39944055
39954056 orientation = 'horizontal' | 'vertical'. If horizontal, barh
3996- will be used and the "bottom" kwarg will be the left.
4057+ will be used and the "bottom" kwarg will be the left edges .
39974058
39984059 width: the width of the bars. If None, automatically compute
39994060 the width.
@@ -4002,12 +4063,14 @@ def hist(self, x, bins=10, normed=0, bottom=0,
40024063 hist bars
40034064 """
40044065 if not self ._hold : self .cla ()
4005- n ,bins = matplotlib .mlab .hist (x , bins , normed )
4066+ n , bins = matplotlib .mlab .hist (x , bins , normed )
40064067 if width is None : width = 0.9 * (bins [1 ]- bins [0 ])
4007- if orientation == 'horizontal' :
4008- patches = self .barh (n , bins , height = width , left = bottom )
4068+ if orientation == 'horizontal' :
4069+ patches = self .barh (bins , n , height = width , left = bottom , align = align )
4070+ elif orientation == 'vertical' :
4071+ patches = self .bar (bins , n , width = width , bottom = bottom , align = align )
40094072 else :
4010- patches = self . bar ( bins , n , width = width , bottom = bottom )
4073+ raise ValueError , 'invalid orientation: %s' % orientation
40114074 for p in patches :
40124075 p .update (kwargs )
40134076 return n , bins , silent_list ('Patch' , patches )
0 commit comments