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

Skip to content

Commit 27bd23b

Browse files
daniel-s-ingramQuLogic
authored andcommitted
Add annotation to AngleMarker
1 parent d34b8d9 commit 27bd23b

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

examples/lines_bars_and_markers/AngleMarker.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import numpy as np
22
import matplotlib.pyplot as plt
33
from matplotlib.patches import Arc
4-
from matplotlib.text import Text
4+
from matplotlib.text import Annotation
55
from matplotlib.transforms import IdentityTransform, TransformedBbox, Bbox
66

77

8-
class AngleMarker(Arc, Text):
8+
class AngleMarker(Arc):
99
"""
1010
Draws an arc between two vectors which appears circular in display space.
1111
"""
@@ -47,12 +47,28 @@ def __init__(self, xy, vec1, vec2, size=100, units="pixels", ax=None,
4747
self.size = size
4848
self.units = units
4949

50+
if self.theta1 > self.theta2:
51+
self.vec1, self.vec2 = self.vec2, self.vec1
52+
5053
Arc.__init__(self, self._xydata, size, size, angle=0.0,
5154
theta1=self.theta1, theta2=self.theta2, **kwargs)
52-
Text.__init__(self, x=self._x, y=self._y, text=text, **kwargs)
53-
5455
self.set_transform(IdentityTransform())
55-
self.ax.add_artist(self)
56+
57+
if units == "pixels" or units == "points":
58+
textcoords = "offset " + units
59+
else:
60+
textcoords = "offset pixels"
61+
62+
annotation = Annotation(
63+
text,
64+
self._xydata,
65+
xytext=self._text_pos,
66+
xycoords="data",
67+
textcoords=textcoords,
68+
**kwargs)
69+
70+
self.ax.add_patch(self)
71+
self.ax.add_artist(annotation)
5672

5773
def get_size(self):
5874
factor = 1.
@@ -91,40 +107,31 @@ def get_theta2(self):
91107
def set_theta(self, angle):
92108
pass
93109

94-
def get_x_text(self):
95-
return self._xydata[0] + 3*self.size
110+
def get_text_pos(self):
111+
theta = np.deg2rad((self.theta2 + self.theta1)/2)
112+
x = self.width*np.cos(theta)
113+
y = self.height*np.sin(theta)
114+
return (x, y)
96115

97-
def get_y_text(self):
98-
return self._xydata[1] + 3*self.size
99-
100-
def set_xy_text(self, xy):
116+
def set_text_pos(self, xy):
101117
pass
102118

103-
def set_color(self, color):
104-
Arc.set_color(self, color)
105-
Text.set_color(self, color)
106-
107-
def draw(self, renderer):
108-
Arc.draw(self, renderer)
109-
Text.draw(self, renderer)
110-
111119
_center = property(get_center_in_pixels, set_center)
112120
theta1 = property(get_theta1, set_theta)
113121
theta2 = property(get_theta2, set_theta)
114122
width = property(get_size, set_size)
115123
height = property(get_size, set_size)
116-
_x = property(get_x_text, set_xy_text)
117-
_y = property(get_y_text, set_xy_text)
124+
_text_pos = property(get_text_pos, set_text_pos)
118125

119126

120127
fig, ax = plt.subplots()
121128

122-
ax.plot([2, .5, 1], [0, .2, 1])
123-
am = AngleMarker((.5, .2), (2, 0), (1, 1), size=0.25, units="axes max", ax=ax,
129+
ax.plot([2, .5, -1], [1, .2, 1])
130+
am = AngleMarker((.5, .2), (2, 1), (-1, 1), size=50, units="pixels", ax=ax,
124131
text=r"$\theta$")
125132
plt.show()
126133

127-
'''
134+
128135
def testing(size=0.25, units="axes fraction", dpi=100, fs=(6.4, 5),
129136
show=False):
130137

@@ -169,4 +176,3 @@ def plot_angle(ax, pos, vec1, vec2, acol="C0", **kwargs):
169176

170177
for (size, unit), dpi, fs in itertools.product(s, d, f):
171178
testing(size=size, units=unit, dpi=dpi, fs=fs)
172-
'''

0 commit comments

Comments
 (0)