Skip to content

Commit 459ae07

Browse files
committed
AnchoredDirectionArrow: first attempt unit test
Currently not working
1 parent f4ec0e2 commit 459ae07

File tree

3 files changed

+96
-69
lines changed

3 files changed

+96
-69
lines changed

lib/mpl_toolkits/axes_grid1/anchored_artists.py

+87-69
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
from matplotlib import docstring, transforms
66
from matplotlib.offsetbox import (AnchoredOffsetbox, AuxTransformBox,
77
DrawingArea, TextArea, VPacker)
8-
from matplotlib.patches import Rectangle, Ellipse, ArrowStyle, FancyArrowPatch, PathPatch
8+
from matplotlib.patches import (Rectangle, Ellipse, ArrowStyle,
9+
FancyArrowPatch, PathPatch)
910
from matplotlib.text import TextPath
1011

1112
__all__ = ['AnchoredDrawingArea', 'AnchoredAuxTransformBox',
12-
'AnchoredEllipse', 'AnchoredSizeBar','AnchoredDirectionArrows']
13+
'AnchoredEllipse', 'AnchoredSizeBar', 'AnchoredDirectionArrows']
1314

1415

1516
class AnchoredDrawingArea(AnchoredOffsetbox):
@@ -375,12 +376,13 @@ def __init__(self, transform, size, label, loc,
375376
prop=fontproperties,
376377
frameon=frameon, **kwargs)
377378

379+
378380
class AnchoredDirectionArrows(AnchoredOffsetbox):
379381
@docstring.dedent
380382
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,
384386
head_width=10, head_length=15, tail_width=2,
385387
text_props=None, arrow_props=None,
386388
**kwargs):
@@ -396,14 +398,16 @@ def __init__(self, transform, label_x, label_y, length=0.15,
396398
label_x, label_y : string
397399
Label text for the x and y arrows
398400
399-
length : int or float
401+
length : int or float, optional
400402
Length of the arrow, given in coordinates of
401403
*transform*.
404+
Defaults to 0.15.
402405
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.
405409
406-
loc : int
410+
loc : int, optional
407411
Location of this size bar. Valid location codes are::
408412
409413
'upper right' : 1,
@@ -417,40 +421,82 @@ def __init__(self, transform, label_x, label_y, length=0.15,
417421
'upper center' : 9,
418422
'center' : 10
419423
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+
420435
pad : int or float, optional
421436
Padding around the label and size bar, in fraction of the font
422-
size. Defaults to 0.1.
437+
size. Defaults to 0.4.
423438
424439
borderpad : int or float, optional
425440
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.
431442
432443
frameon : bool, optional
433444
If True, draw a box around the horizontal bar and label.
434445
Defaults to False.
435446
436447
color : str, optional
437448
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.
439458
440459
fontproperties : `matplotlib.font_manager.FontProperties`, optional
441460
Font properties for the label text.
442461
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+
443483
**kwargs :
444484
Keyworded arguments to pass to
445485
:class:`matplotlib.offsetbox.AnchoredOffsetbox`.
446486
447487
Attributes
448488
----------
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
451491
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.
454500
455501
Notes
456502
-----
@@ -466,18 +512,19 @@ def __init__(self, transform, label_x, label_y, length=0.15,
466512
AnchoredDirectionArrows
467513
>>> fig, ax = plt.subplots()
468514
>>> 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)
471518
>>> fig.show()
472519
473-
Using all the optional parameters
520+
Using several of the optional parameters, creating downward pointing
521+
arrow and high contrast text labels.
474522
475523
>>> 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)
481528
"""
482529
if arrow_props is None:
483530
arrow_props = {}
@@ -486,15 +533,13 @@ def __init__(self, transform, label_x, label_y, length=0.15,
486533
text_props = {}
487534

488535
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)
492539

493540
if fontproperties is None and 'prop' in kwargs:
494541
fontproperties = kwargs.pop('prop')
495542

496-
print(bool(arrow_props))
497-
498543
if 'color' not in arrow_props:
499544
arrow_props['color'] = color
500545

@@ -508,64 +553,38 @@ def __init__(self, transform, label_x, label_y, length=0.15,
508553
text_props['alpha'] = alpha
509554

510555
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)
514557

515558
self._box = AuxTransformBox(t_end)
516559

517560
length_x = length
518561
length_y = length*aspect_ratio
519562

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),
523566
arrowstyle=arrowstyle,
524567
shrinkA=0.0,
525568
shrinkB=0.0,
526569
**arrow_props)
527-
self.arrow_x_face.set_ec('None')
528570

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),
532574
arrowstyle=arrowstyle,
533575
shrinkA=0.0,
534576
shrinkB=0.0,
535577
**arrow_props)
536-
self.arrow_x_edge.set_fc('None')
537578

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)
546581

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
562582
text_path_x = TextPath((
563583
length_x+sep_x, back_length*length_y+sep_y), label_x,
564584
size=fontsize, prop=fontproperties)
565585
self.p_x = PathPatch(text_path_x, transform=t_start, **text_props)
566586
self._box.add_artist(self.p_x)
567587

568-
#Label Y
569588
text_path_y = TextPath((
570589
length_x*back_length+sep_x, length_y*(1-back_length)+sep_y),
571590
label_y, size=fontsize, prop=fontproperties)
@@ -575,4 +594,3 @@ def __init__(self, transform, label_x, label_y, length=0.15,
575594
AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad,
576595
child=self._box,
577596
frameon=frameon, **kwargs)
578-

lib/mpl_toolkits/tests/test_axes_grid1.py

+9
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,12 @@ def test_zooming_with_inverted_axes():
171171
ax.axis([3, 1, 3, 1])
172172
inset_ax = zoomed_inset_axes(ax, zoom=2.5, loc=4)
173173
inset_ax.axis([1.4, 1.1, 1.4, 1.1])
174+
175+
@image_comparison(baseline_images=['anchored_direction_arrows'],
176+
extensions=['png'])
177+
def test_anchored_direction_arrows():
178+
fig, ax = plt.subplots()
179+
ax.imshow(np.zeros((10,10)))
180+
181+
simple_arrow = AnchoredDirectionArrows(ax.transAxes, 'X','Y')
182+
ax.add_artist(simple_arrow)

0 commit comments

Comments
 (0)