Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5725c83

Browse files
daniel-s-ingramQuLogic
authored andcommitted
Made radius scale invariant
1 parent b6134dc commit 5725c83

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

examples/lines_bars_and_markers/angle_arc.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
vec2 = ax.arrow(0, 0, -1, 2, head_width=0.05)
1616

1717

18-
def get_vector_angle(vec):
18+
def get_vector_angle(trans, vec):
1919
"""
2020
Finds the angle of a FancyArrow. Probably
2121
a better way to do this.
2222
2323
Parameters
2424
----------
25+
trans : matplotlib.transforms.CompositeGenericTransform
26+
Transformation from data coords to axes coords
2527
vec : matplotlib.patches.FancyArrow
2628
2729
Returns
@@ -30,7 +32,8 @@ def get_vector_angle(vec):
3032
Angle in radians between +x axis
3133
and vec.
3234
"""
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
3437
dx = max(xy[:, 0], key=abs) - min(xy[:, 0], key=abs)
3538
dy = max(xy[:, 1], key=abs) - min(xy[:, 1], key=abs)
3639
return atan2(dy, dx)*180/pi
@@ -54,20 +57,22 @@ def draw_arc_between_vectors(ax, vec1, vec2):
5457
vec2 : matplotlib.patches.FancyArrow
5558
Vector 2
5659
"""
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))
5962
dx = x1 - x0
6063
dy = y1 - y0
6164
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)
6970
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)
7176
[p.remove() for p in ax.patches if p.get_gid() == "angle_arc"]
7277
ax.add_patch(arc)
7378

0 commit comments

Comments
 (0)