15
15
vec2 = ax .arrow (0 , 0 , - 1 , 2 , head_width = 0.05 )
16
16
17
17
18
- def get_vector_angle (vec ):
18
+ def get_vector_angle (trans , vec ):
19
19
"""
20
20
Finds the angle of a FancyArrow. Probably
21
21
a better way to do this.
22
22
23
23
Parameters
24
24
----------
25
+ trans : matplotlib.transforms.CompositeGenericTransform
26
+ Transformation from data coords to axes coords
25
27
vec : matplotlib.patches.FancyArrow
26
28
27
29
Returns
@@ -30,7 +32,8 @@ def get_vector_angle(vec):
30
32
Angle in radians between +x axis
31
33
and vec.
32
34
"""
33
- xy = vec .get_xy ()
35
+ #Shift axes coords to -0.5, 0.5 for proper angle calculation
36
+ xy = trans .transform (vec .get_xy ()) - 0.5
34
37
dx = max (xy [:, 0 ], key = abs ) - min (xy [:, 0 ], key = abs )
35
38
dy = max (xy [:, 1 ], key = abs ) - min (xy [:, 1 ], key = abs )
36
39
return atan2 (dy , dx )* 180 / pi
@@ -54,20 +57,22 @@ def draw_arc_between_vectors(ax, vec1, vec2):
54
57
vec2 : matplotlib.patches.FancyArrow
55
58
Vector 2
56
59
"""
57
- x0 , y0 = ax .transData .transform ((0 , 0 ))
58
- x1 , y1 = ax .transData .transform ((1 , 1 ))
60
+ x0 , y0 = ax .transAxes .transform ((0 , 0 ))
61
+ x1 , y1 = ax .transAxes .transform ((1 , 1 ))
59
62
dx = x1 - x0
60
63
dy = y1 - y0
61
64
d = sqrt (dx ** 2 + dy ** 2 )
62
- width = d / dx
63
- height = d / dy
64
- norm = sqrt (width ** 2 + height ** 2 )
65
- width /= norm
66
- height /= norm
67
- theta1 = get_vector_angle (vec1 )
68
- theta2 = get_vector_angle (vec2 )
65
+ width = 0.1 * d / dx
66
+ height = 0.1 * d / dy
67
+ trans = ax .transData + ax .transAxes .inverted ()
68
+ theta1 = get_vector_angle (trans , vec1 )
69
+ theta2 = get_vector_angle (trans , vec2 )
69
70
arc = mpatches .Arc (
70
- (0 , 0 ), width , height , theta1 = theta1 , theta2 = theta2 , gid = "angle_arc" )
71
+ trans .transform ((0 , 0 )),
72
+ width , height ,
73
+ theta1 = theta1 , theta2 = theta2 ,
74
+ gid = "angle_arc" ,
75
+ transform = ax .transAxes )
71
76
[p .remove () for p in ax .patches if p .get_gid () == "angle_arc" ]
72
77
ax .add_patch (arc )
73
78
0 commit comments