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

Skip to content

Commit e65100a

Browse files
committed
Add bar connection style for annotation
svn path=/trunk/matplotlib/; revision=6999
1 parent 6d26054 commit e65100a

4 files changed

Lines changed: 101 additions & 0 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2009-03-20 Add "bar" connection style for annotation - JJL
2+
13
2009-03-17 Fix bugs in edge color handling by contourf, found
24
by Jae-Joon Lee. - EF
35

examples/pylab_examples/annotation_demo2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@
7171
)
7272

7373

74+
ann = ax.annotate('', xy=(4., 1.), xycoords='data',
75+
xytext=(4.5, -1), textcoords='data',
76+
arrowprops=dict(arrowstyle="<->",
77+
connectionstyle="bar",
78+
ec="k",
79+
shrinkA=5, shrinkB=5,
80+
)
81+
)
7482

7583

7684
if 1:
@@ -144,4 +152,5 @@
144152
)
145153
)
146154

155+
147156
show()

lib/matplotlib/patches.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,92 @@ def connect(self, posA, posB):
25452545

25462546
_style_list["arc"] = Arc
25472547

2548+
2549+
2550+
class Bar(_Base):
2551+
"""
2552+
A line with *angle* between A and B with *armA* and
2553+
*armB*. One of the arm is extend so that they are connected in
2554+
a right angle. The length of armA is determined by (*armA*
2555+
+ *fraction* x AB distance). Same for armB.
2556+
"""
2557+
2558+
def __init__(self, armA=0., armB=0., fraction=0.3, angle=None):
2559+
"""
2560+
*armA* : minimum length of armA
2561+
*armB* : minimum length of armB
2562+
*fraction* : a fraction of the distance between two points that will be added to armA and armB.
2563+
*angle* : anlge of the connecting line (if None, parallel to A and B)
2564+
"""
2565+
self.armA = armA
2566+
self.armB = armB
2567+
self.fraction = fraction
2568+
self.angle = angle
2569+
2570+
def connect(self, posA, posB):
2571+
x1, y1 = posA
2572+
x20, y20 = x2, y2 = posB
2573+
2574+
x12, y12 = (x1 + x2)/2., (y1 + y2)/2.
2575+
2576+
theta1 = math.atan2(y2-y1, x2-x1)
2577+
dx, dy = x2 - x1, y2 - y1
2578+
dd = (dx*dx + dy*dy)**.5
2579+
ddx, ddy = dx/dd, dy/dd
2580+
2581+
armA, armB = self.armA, self.armB
2582+
2583+
if self.angle is not None:
2584+
#angle = self.angle % 180.
2585+
#if angle < 0. or angle > 180.:
2586+
# angle
2587+
theta0 = (self.angle%180.)/180.*math.pi
2588+
#theta0 = (((self.angle+90)%180.) - 90.)/180.*math.pi
2589+
dtheta = theta1 - theta0
2590+
dl = dd*math.sin(dtheta)
2591+
2592+
dL = dd*math.cos(dtheta)
2593+
2594+
#x2, y2 = x2 + dl*ddy, y2 - dl*ddx
2595+
x2, y2 = x1 + dL*math.cos(theta0), y1 + dL*math.sin(theta0)
2596+
2597+
armB = armB - dl
2598+
2599+
# update
2600+
dx, dy = x2 - x1, y2 - y1
2601+
dd2 = (dx*dx + dy*dy)**.5
2602+
ddx, ddy = dx/dd2, dy/dd2
2603+
2604+
else:
2605+
dl = 0.
2606+
2607+
#if armA > armB:
2608+
# armB = armA + dl
2609+
#else:
2610+
# armA = armB - dl
2611+
2612+
2613+
arm = max(armA, armB)
2614+
f = self.fraction*dd + arm
2615+
#fB = self.fraction*dd + armB
2616+
2617+
cx1, cy1 = x1 + f*ddy, y1 - f*ddx
2618+
cx2, cy2 = x2 + f*ddy, y2 - f*ddx
2619+
2620+
vertices = [(x1, y1),
2621+
(cx1, cy1),
2622+
(cx2, cy2),
2623+
(x20, y20)]
2624+
codes = [Path.MOVETO,
2625+
Path.LINETO,
2626+
Path.LINETO,
2627+
Path.LINETO]
2628+
2629+
return Path(vertices, codes)
2630+
2631+
_style_list["bar"] = Bar
2632+
2633+
25482634
__doc__ = cbook.dedent(__doc__) % \
25492635
{"AvailableConnectorstyles": _pprint_styles(_style_list)}
25502636

lib/matplotlib/text.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ def update_bbox_position_size(self, renderer):
406406
props = props.copy() # don't want to alter the pad externally
407407
pad = props.pop('pad', 4)
408408
pad = renderer.points_to_pixels(pad)
409+
if self._text == "":
410+
self.arrow_patch.set_patchA(None)
411+
return
412+
409413
bbox = self.get_window_extent(renderer)
410414
l,b,w,h = bbox.bounds
411415
l-=pad/2.

0 commit comments

Comments
 (0)