|
1 | 1 | import numpy as np
|
2 | 2 | import matplotlib.pyplot as plt
|
3 | 3 | from matplotlib.patches import Arc
|
4 |
| -from matplotlib.text import Text |
| 4 | +from matplotlib.text import Annotation |
5 | 5 | from matplotlib.transforms import IdentityTransform, TransformedBbox, Bbox
|
6 | 6 |
|
7 | 7 |
|
8 |
| -class AngleMarker(Arc, Text): |
| 8 | +class AngleMarker(Arc): |
9 | 9 | """
|
10 | 10 | Draws an arc between two vectors which appears circular in display space.
|
11 | 11 | """
|
@@ -47,12 +47,28 @@ def __init__(self, xy, vec1, vec2, size=100, units="pixels", ax=None,
|
47 | 47 | self.size = size
|
48 | 48 | self.units = units
|
49 | 49 |
|
| 50 | + if self.theta1 > self.theta2: |
| 51 | + self.vec1, self.vec2 = self.vec2, self.vec1 |
| 52 | + |
50 | 53 | Arc.__init__(self, self._xydata, size, size, angle=0.0,
|
51 | 54 | theta1=self.theta1, theta2=self.theta2, **kwargs)
|
52 |
| - Text.__init__(self, x=self._x, y=self._y, text=text, **kwargs) |
53 |
| - |
54 | 55 | 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) |
56 | 72 |
|
57 | 73 | def get_size(self):
|
58 | 74 | factor = 1.
|
@@ -91,40 +107,31 @@ def get_theta2(self):
|
91 | 107 | def set_theta(self, angle):
|
92 | 108 | pass
|
93 | 109 |
|
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) |
96 | 115 |
|
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): |
101 | 117 | pass
|
102 | 118 |
|
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 |
| - |
111 | 119 | _center = property(get_center_in_pixels, set_center)
|
112 | 120 | theta1 = property(get_theta1, set_theta)
|
113 | 121 | theta2 = property(get_theta2, set_theta)
|
114 | 122 | width = property(get_size, set_size)
|
115 | 123 | 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) |
118 | 125 |
|
119 | 126 |
|
120 | 127 | fig, ax = plt.subplots()
|
121 | 128 |
|
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, |
124 | 131 | text=r"$\theta$")
|
125 | 132 | plt.show()
|
126 | 133 |
|
127 |
| -''' |
| 134 | + |
128 | 135 | def testing(size=0.25, units="axes fraction", dpi=100, fs=(6.4, 5),
|
129 | 136 | show=False):
|
130 | 137 |
|
@@ -169,4 +176,3 @@ def plot_angle(ax, pos, vec1, vec2, acol="C0", **kwargs):
|
169 | 176 |
|
170 | 177 | for (size, unit), dpi, fs in itertools.product(s, d, f):
|
171 | 178 | testing(size=size, units=unit, dpi=dpi, fs=fs)
|
172 |
| -''' |
|
0 commit comments