5
5
from matplotlib import docstring , transforms
6
6
from matplotlib .offsetbox import (AnchoredOffsetbox , AuxTransformBox ,
7
7
DrawingArea , TextArea , VPacker )
8
- from matplotlib .patches import Rectangle , Ellipse , ArrowStyle , FancyArrowPatch , PathPatch
8
+ from matplotlib .patches import (Rectangle , Ellipse , ArrowStyle ,
9
+ FancyArrowPatch , PathPatch )
9
10
from matplotlib .text import TextPath
10
11
11
12
__all__ = ['AnchoredDrawingArea' , 'AnchoredAuxTransformBox' ,
12
- 'AnchoredEllipse' , 'AnchoredSizeBar' ,'AnchoredDirectionArrows' ]
13
+ 'AnchoredEllipse' , 'AnchoredSizeBar' , 'AnchoredDirectionArrows' ]
13
14
14
15
15
16
class AnchoredDrawingArea (AnchoredOffsetbox ):
@@ -375,12 +376,13 @@ def __init__(self, transform, size, label, loc,
375
376
prop = fontproperties ,
376
377
frameon = frameon , ** kwargs )
377
378
379
+
378
380
class AnchoredDirectionArrows (AnchoredOffsetbox ):
379
381
@docstring .dedent
380
382
def __init__ (self , transform , label_x , label_y , length = 0.15 ,
381
- fontsize = 0.08 , loc = 2 , angle = 0 , aspect_ratio = 1 , pad = 0.1 ,
382
- borderpad = 0.1 , frameon = False , color = 'w' , alpha = 1 ,
383
- sep_x = 0.01 ,sep_y = 0 , fontproperties = None , back_length = 0.15 ,
383
+ fontsize = 0.08 , loc = 2 , angle = 0 , aspect_ratio = 1 , pad = 0.4 ,
384
+ borderpad = 0.4 , frameon = False , color = 'w' , alpha = 1 ,
385
+ sep_x = 0.01 , sep_y = 0 , fontproperties = None , back_length = 0.15 ,
384
386
head_width = 10 , head_length = 15 , tail_width = 2 ,
385
387
text_props = None , arrow_props = None ,
386
388
** kwargs ):
@@ -396,14 +398,16 @@ def __init__(self, transform, label_x, label_y, length=0.15,
396
398
label_x, label_y : string
397
399
Label text for the x and y arrows
398
400
399
- length : int or float
401
+ length : int or float, optional
400
402
Length of the arrow, given in coordinates of
401
403
*transform*.
404
+ Defaults to 0.15.
402
405
403
- label : str
404
- Label to display.
406
+ fontsize : int, optional
407
+ Size of label strings, given in coordinates of *transform*.
408
+ Defaults to 0.08.
405
409
406
- loc : int
410
+ loc : int, optional
407
411
Location of this size bar. Valid location codes are::
408
412
409
413
'upper right' : 1,
@@ -417,40 +421,82 @@ def __init__(self, transform, label_x, label_y, length=0.15,
417
421
'upper center' : 9,
418
422
'center' : 10
419
423
424
+ Defaults to 2.
425
+
426
+ angle : int or float, optional
427
+ The angle of the arrows in degrees.
428
+ Defaults to 0.
429
+
430
+ aspect_ratio : int or float, optional
431
+ The ratio of the length of arrow_x and arrow_y.
432
+ Negative numbers can be used to change the direction.
433
+ Defaults to 1.
434
+
420
435
pad : int or float, optional
421
436
Padding around the label and size bar, in fraction of the font
422
- size. Defaults to 0.1 .
437
+ size. Defaults to 0.4 .
423
438
424
439
borderpad : int or float, optional
425
440
Border padding, in fraction of the font size.
426
- Defaults to 0.1.
427
-
428
- sep_x : int or float, optional
429
- Separation between the arrows and labels in ???
430
- Defaults to 0.01.
441
+ Defaults to 0.4.
431
442
432
443
frameon : bool, optional
433
444
If True, draw a box around the horizontal bar and label.
434
445
Defaults to False.
435
446
436
447
color : str, optional
437
448
Color for the size bar and label.
438
- Defaults to black.
449
+ Defaults to white.
450
+
451
+ alpha : int or float, optional
452
+ Alpha values of the arrows and labels
453
+ Defaults to 1.
454
+
455
+ sep_x, sep_y : int or float, optional
456
+ Separation between the arrows and labels in coordinates of
457
+ *transform*. Defaults to 0.01 and 0.
439
458
440
459
fontproperties : `matplotlib.font_manager.FontProperties`, optional
441
460
Font properties for the label text.
442
461
462
+ back_length : float, optional
463
+ Fraction of the arrow behind the arrow crossing.
464
+ Defaults to 0.15.
465
+
466
+ head_width : int or float, optional
467
+ Width of arrow head, sendt to ArrowStyle.
468
+ Defaults to 10.
469
+
470
+ head_length : int or float, optional
471
+ Length of arrow head, sendt to ArrowStyle.
472
+ Defaults to 15.
473
+
474
+ tail_width : int or float, optional
475
+ Width of arrow tail, sendt to ArrowStyle.
476
+ Defaults to 2.
477
+
478
+ text_props, arrow_props : dict
479
+ Properties of the text and arrows, passed to :class:
480
+ `matplotlib.text.TextPath` and
481
+ `matplotlib.patches.FancyArrowPatch`
482
+
443
483
**kwargs :
444
484
Keyworded arguments to pass to
445
485
:class:`matplotlib.offsetbox.AnchoredOffsetbox`.
446
486
447
487
Attributes
448
488
----------
449
- size_bar : `matplotlib.offsetbox.AuxTransformBox `
450
- Container for the size bar.
489
+ arrow_x, arrow_y : `matplotlib.patches.FancyArrowPatch `
490
+ Arrow x and y
451
491
452
- txt_label : `matplotlib.offsetbox.TextArea`
453
- Container for the label of the size bar.
492
+ text_path_x, text_path_y : `matplotlib.text.TextPath`
493
+ Path for arrow labels
494
+
495
+ p_x, p_y : `matplotlib.patches.PathPatch`
496
+ Patch for arrow labels
497
+
498
+ _box : `matplotlib.offsetbox.AuxTransformBox`
499
+ Container for the arrows and labels.
454
500
455
501
Notes
456
502
-----
@@ -466,18 +512,19 @@ def __init__(self, transform, label_x, label_y, length=0.15,
466
512
AnchoredDirectionArrows
467
513
>>> fig, ax = plt.subplots()
468
514
>>> ax.imshow(np.random.random((10,10)))
469
- >>> arrows = AnchoredDirectionArrows(ax.transData, 3, '3 data units', 4)
470
- >>> ax.add_artist(bar)
515
+ >>> arrows = AnchoredDirectionArrows(ax.transAxes, '111', \
516
+ r'11$\overline{2}$')
517
+ >>> ax.add_artist(arrows)
471
518
>>> fig.show()
472
519
473
- Using all the optional parameters
520
+ Using several of the optional parameters, creating downward pointing
521
+ arrow and high contrast text labels.
474
522
475
523
>>> import matplotlib.font_manager as fm
476
- >>> fontprops = fm.FontProperties(size=14, family='monospace')
477
- >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, \
478
- sep=5, borderpad=0.5, frameon=False, \
479
- size_vertical=0.5, color='white', \
480
- fontproperties=fontprops)
524
+ >>> fontprops = fm.FontProperties(family='monospace')
525
+ >>> arrows = AnchoredDirectionArrows(ax.transAxes,'East','South', \
526
+ loc='lower left', color='k', aspect_ratio=-1, sep_x=0.02,sep_y=-0.01, \
527
+ text_props={'ec':'w','fc':'k'}, fontproperties=fontprops)
481
528
"""
482
529
if arrow_props is None :
483
530
arrow_props = {}
@@ -486,15 +533,13 @@ def __init__(self, transform, label_x, label_y, length=0.15,
486
533
text_props = {}
487
534
488
535
arrowstyle = ArrowStyle ("Simple" ,
489
- head_width = head_width ,
490
- head_length = head_length ,
491
- tail_width = tail_width )
536
+ head_width = head_width ,
537
+ head_length = head_length ,
538
+ tail_width = tail_width )
492
539
493
540
if fontproperties is None and 'prop' in kwargs :
494
541
fontproperties = kwargs .pop ('prop' )
495
542
496
- print (bool (arrow_props ))
497
-
498
543
if 'color' not in arrow_props :
499
544
arrow_props ['color' ] = color
500
545
@@ -508,64 +553,38 @@ def __init__(self, transform, label_x, label_y, length=0.15,
508
553
text_props ['alpha' ] = alpha
509
554
510
555
t_start = transform
511
- t = transforms .Affine2D ().rotate_deg (angle )
512
- t_end = t_start + t
513
- t_text = t_start #_end + Affine2D().rotate_deg(angle)
556
+ t_end = t_start + transforms .Affine2D ().rotate_deg (angle )
514
557
515
558
self ._box = AuxTransformBox (t_end )
516
559
517
560
length_x = length
518
561
length_y = length * aspect_ratio
519
562
520
- self .arrow_x_face = FancyArrowPatch (
521
- (0 ,back_length * length_y ),
522
- (length_x ,back_length * length_y ),
563
+ self .arrow_x = FancyArrowPatch (
564
+ (0 , back_length * length_y ),
565
+ (length_x , back_length * length_y ),
523
566
arrowstyle = arrowstyle ,
524
567
shrinkA = 0.0 ,
525
568
shrinkB = 0.0 ,
526
569
** arrow_props )
527
- self .arrow_x_face .set_ec ('None' )
528
570
529
- self .arrow_x_edge = FancyArrowPatch (
530
- (0 , back_length * length_y ),
531
- (length_x , back_length * length_y ),
571
+ self .arrow_y = FancyArrowPatch (
572
+ (back_length * length_x , 0 ),
573
+ (back_length * length_x , length_y ),
532
574
arrowstyle = arrowstyle ,
533
575
shrinkA = 0.0 ,
534
576
shrinkB = 0.0 ,
535
577
** arrow_props )
536
- self .arrow_x_edge .set_fc ('None' )
537
578
538
- self .arrow_y_face = FancyArrowPatch (
539
- (back_length * length_x ,0 ),
540
- (back_length * length_x ,length_y ),
541
- arrowstyle = arrowstyle ,
542
- shrinkA = 0.0 ,
543
- shrinkB = 0.0 ,
544
- ** arrow_props )
545
- self .arrow_y_face .set_ec ('None' )
579
+ self ._box .add_artist (self .arrow_x )
580
+ self ._box .add_artist (self .arrow_y )
546
581
547
- self .arrow_y_edge = FancyArrowPatch (
548
- (back_length * length_x ,0 ),
549
- (back_length * length_x ,length_y ),
550
- arrowstyle = arrowstyle ,
551
- shrinkA = 0.0 ,
552
- shrinkB = 0.0 ,
553
- ** arrow_props )
554
- self .arrow_y_edge .set_fc ('None' )
555
-
556
- self ._box .add_artist (self .arrow_x_edge )
557
- self ._box .add_artist (self .arrow_y_edge )
558
- self ._box .add_artist (self .arrow_x_face )
559
- self ._box .add_artist (self .arrow_y_face )
560
-
561
- #Label X
562
582
text_path_x = TextPath ((
563
583
length_x + sep_x , back_length * length_y + sep_y ), label_x ,
564
584
size = fontsize , prop = fontproperties )
565
585
self .p_x = PathPatch (text_path_x , transform = t_start , ** text_props )
566
586
self ._box .add_artist (self .p_x )
567
587
568
- #Label Y
569
588
text_path_y = TextPath ((
570
589
length_x * back_length + sep_x , length_y * (1 - back_length )+ sep_y ),
571
590
label_y , size = fontsize , prop = fontproperties )
@@ -575,4 +594,3 @@ def __init__(self, transform, label_x, label_y, length=0.15,
575
594
AnchoredOffsetbox .__init__ (self , loc , pad = pad , borderpad = borderpad ,
576
595
child = self ._box ,
577
596
frameon = frameon , ** kwargs )
578
-
0 commit comments