@@ -2364,180 +2364,238 @@ def get_handles():
2364
2364
2365
2365
2366
2366
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'
2368
2369
):
2369
2370
"""
2370
2371
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')
2372
2374
2373
- Make a bar plot with rectangles at
2375
+ Make a bar plot with rectangles bounded by
2374
2376
2375
- left, left+width, 0, height
2377
+ left, left+width, bottom, bottom+ height (left, right, bottom and top edges)
2376
2378
2377
- left and height are Numeric arrays.
2379
+ left, height, width, and bottom can be either scalars or sequences
2378
2380
2379
2381
Return value is a list of Rectangle patch instances
2380
2382
2381
2383
BAR(left, height, width, bottom,
2382
- color, yerr, xerr, capsize, yoff)
2384
+ color, edgecolor, yerr, xerr, ecolor, capsize,
2385
+ align, orientation)
2383
2386
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
2386
2394
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
2388
2403
2389
2404
ecolor specifies the color of any errorbar
2390
2405
2391
2406
capsize determines the length in points of the error bar caps
2392
2407
2408
+ align = 'edge' | 'center'
2409
+
2410
+ orientation = 'vertical' | 'horizontal'
2393
2411
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
2396
2419
2397
2420
This enables you to use bar as the basis for stacked bar
2398
2421
charts, or candlestick plots
2399
2422
"""
2400
2423
if not self ._hold : self .cla ()
2401
2424
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
+
2403
2454
left = asarray (left )
2404
2455
height = asarray (height )
2405
-
2406
- patches = []
2407
-
2456
+ width = asarray (width )
2457
+ bottom = asarray (bottom )
2408
2458
2409
2459
# 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
2411
2461
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
2413
2463
not iterable (color )):
2414
- color = [color ]* len ( left )
2464
+ color = [color ]* nbars
2415
2465
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
2416
2472
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
2419
2475
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 )
2423
2479
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 = []
2425
2496
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
2431
2506
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 :
2434
2509
if h < 0 :
2435
2510
b += h
2436
2511
h = abs (h )
2437
2512
r = Rectangle (
2438
2513
xy = (l , b ), width = w , height = h ,
2439
2514
facecolor = c ,
2515
+ edgecolor = e ,
2440
2516
)
2441
2517
self .add_patch (r )
2442
2518
patches .append (r )
2443
2519
2520
+ holdstate = self ._hold
2521
+ self .hold (True ) # ensure hold is on before plotting errorbars
2444
2522
2445
2523
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
2446
2528
self .errorbar (
2447
- left + 0.5 * width , bottom + height ,
2529
+ x , y ,
2448
2530
yerr = yerr , xerr = xerr ,
2449
2531
fmt = None , ecolor = ecolor , capsize = capsize )
2532
+
2533
+ self .hold (holdstate ) # restore previous hold state
2534
+
2450
2535
self .autoscale_view ()
2451
2536
return patches
2452
2537
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)
2461
2538
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')
2464
2547
2465
- Return value is a list of Rectangle patch instances
2548
+ Make a horizontal bar plot with rectangles bounded by
2466
2549
2467
- Optional arguments
2550
+ left, left+width, bottom, bottom+height (left, right, bottom and top edges)
2468
2551
2469
- height - the height (thickness) of the bar
2552
+ bottom, width, height, and left can be either scalars or sequences
2470
2553
2471
- left - the x coordinate of the left side of the bar
2554
+ Return value is a list of Rectangle patch instances
2472
2555
2473
- color specifies the color of the bar
2556
+ BARH(bottom, width, height, left,
2557
+ color, edgecolor, xerr, yerr, ecolor, capsize,
2558
+ align)
2474
2559
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
2477
2561
2478
- ecolor specifies the color of any errorbar
2562
+ width - the lengths of the bars
2479
2563
2480
- capsize determines the length in points of the error bar caps
2564
+ Optional arguments
2481
2565
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
2486
2567
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
2490
2569
2491
- patches = []
2570
+ color specifies the colors of the bars
2492
2571
2572
+ edgecolor specifies the colors of the bar edges
2493
2573
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
2500
2576
2577
+ ecolor specifies the color of any errorbar
2501
2578
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
2510
2580
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.
2516
2584
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
2520
2587
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
+ """
2532
2591
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
+ )
2539
2596
return patches
2540
2597
2598
+
2541
2599
def stem (self , x , y , linefmt = 'b-' , markerfmt = 'bo' , basefmt = 'r-' ):
2542
2600
"""
2543
2601
STEM(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')
@@ -3978,9 +4036,10 @@ def table(self, **kwargs):
3978
4036
#### Data analysis
3979
4037
3980
4038
def hist (self , x , bins = 10 , normed = 0 , bottom = 0 ,
3981
- orientation = 'vertical' , width = None , ** kwargs ):
4039
+ align = 'edge' , orientation = 'vertical' , width = None , ** kwargs ):
3982
4040
"""
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)
3984
4043
3985
4044
Compute the histogram of x. bins is either an integer number of
3986
4045
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,
3991
4050
be the counts normalized to form a probability density, ie,
3992
4051
n/(len(x)*dbin)
3993
4052
4053
+ align = 'edge' | 'center'. Interprets bins either as edge
4054
+ or center values
3994
4055
3995
4056
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 .
3997
4058
3998
4059
width: the width of the bars. If None, automatically compute
3999
4060
the width.
@@ -4002,12 +4063,14 @@ def hist(self, x, bins=10, normed=0, bottom=0,
4002
4063
hist bars
4003
4064
"""
4004
4065
if not self ._hold : self .cla ()
4005
- n ,bins = matplotlib .mlab .hist (x , bins , normed )
4066
+ n , bins = matplotlib .mlab .hist (x , bins , normed )
4006
4067
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 )
4009
4072
else :
4010
- patches = self . bar ( bins , n , width = width , bottom = bottom )
4073
+ raise ValueError , 'invalid orientation: %s' % orientation
4011
4074
for p in patches :
4012
4075
p .update (kwargs )
4013
4076
return n , bins , silent_list ('Patch' , patches )
0 commit comments