Revision: 6999
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6999&view=rev
Author:   leejjoon
Date:     2009-03-20 20:10:42 +0000 (Fri, 20 Mar 2009)

Log Message:
-----------
Add bar connection style for annotation

Modified Paths:
--------------
    trunk/matplotlib/CHANGELOG
    trunk/matplotlib/examples/pylab_examples/annotation_demo2.py
    trunk/matplotlib/lib/matplotlib/patches.py
    trunk/matplotlib/lib/matplotlib/text.py

Modified: trunk/matplotlib/CHANGELOG
===================================================================
--- trunk/matplotlib/CHANGELOG  2009-03-19 14:44:01 UTC (rev 6998)
+++ trunk/matplotlib/CHANGELOG  2009-03-20 20:10:42 UTC (rev 6999)
@@ -1,3 +1,5 @@
+2009-03-20 Add "bar" connection style for annotation - JJL
+
 2009-03-17 Fix bugs in edge color handling by contourf, found
            by Jae-Joon Lee. - EF
 

Modified: trunk/matplotlib/examples/pylab_examples/annotation_demo2.py
===================================================================
--- trunk/matplotlib/examples/pylab_examples/annotation_demo2.py        
2009-03-19 14:44:01 UTC (rev 6998)
+++ trunk/matplotlib/examples/pylab_examples/annotation_demo2.py        
2009-03-20 20:10:42 UTC (rev 6999)
@@ -71,6 +71,14 @@
                 )
 
 
+    ann = ax.annotate('', xy=(4., 1.),  xycoords='data',
+                xytext=(4.5, -1), textcoords='data',
+                arrowprops=dict(arrowstyle="<->",
+                                connectionstyle="bar",
+                                ec="k",
+                                shrinkA=5, shrinkB=5,
+                                )
+                )
 
 
 if 1:
@@ -144,4 +152,5 @@
                                 )
                 )
 
+
 show()

Modified: trunk/matplotlib/lib/matplotlib/patches.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/patches.py  2009-03-19 14:44:01 UTC (rev 
6998)
+++ trunk/matplotlib/lib/matplotlib/patches.py  2009-03-20 20:10:42 UTC (rev 
6999)
@@ -2545,6 +2545,92 @@
 
     _style_list["arc"] = Arc
 
+
+
+    class Bar(_Base):
+        """
+        A line with *angle* between A and B with *armA* and
+        *armB*. One of the arm is extend so that they are connected in
+        a right angle. The length of armA is determined by (*armA*
+        + *fraction* x AB distance). Same for armB.
+        """
+
+        def __init__(self, armA=0., armB=0., fraction=0.3, angle=None):
+            """
+            *armA* : minimum length of armA
+            *armB* : minimum length of armB
+            *fraction* : a fraction of the distance between two points that 
will be added to armA and armB.
+            *angle* : anlge of the connecting line (if None, parallel to A and 
B)
+            """
+            self.armA = armA
+            self.armB = armB
+            self.fraction = fraction
+            self.angle = angle
+
+        def connect(self, posA, posB):
+            x1, y1 = posA
+            x20, y20 = x2, y2 = posB
+             
+            x12, y12 = (x1 + x2)/2., (y1 + y2)/2.
+
+            theta1 = math.atan2(y2-y1, x2-x1)
+            dx, dy = x2 - x1, y2 - y1
+            dd = (dx*dx + dy*dy)**.5
+            ddx, ddy = dx/dd, dy/dd
+
+            armA, armB = self.armA, self.armB
+            
+            if self.angle is not None:
+                #angle = self.angle % 180.
+                #if angle < 0. or angle > 180.:
+                #    angle
+                theta0 = (self.angle%180.)/180.*math.pi
+                #theta0 = (((self.angle+90)%180.)  - 90.)/180.*math.pi
+                dtheta = theta1 - theta0
+                dl = dd*math.sin(dtheta)
+
+                dL = dd*math.cos(dtheta)
+
+                #x2, y2 = x2 + dl*ddy, y2 - dl*ddx
+                x2, y2 = x1 + dL*math.cos(theta0), y1 + dL*math.sin(theta0)
+
+                armB = armB - dl
+
+                # update
+                dx, dy = x2 - x1, y2 - y1
+                dd2 = (dx*dx + dy*dy)**.5
+                ddx, ddy = dx/dd2, dy/dd2
+
+            else:
+                dl = 0.
+                
+            #if armA > armB:
+            #    armB = armA + dl
+            #else:
+            #    armA = armB - dl
+
+
+            arm = max(armA, armB)
+            f = self.fraction*dd + arm
+            #fB = self.fraction*dd + armB
+
+            cx1, cy1 = x1 + f*ddy, y1 - f*ddx
+            cx2, cy2 = x2 + f*ddy, y2 - f*ddx
+
+            vertices = [(x1, y1),
+                        (cx1, cy1),
+                        (cx2, cy2),
+                        (x20, y20)]
+            codes = [Path.MOVETO,
+                     Path.LINETO,
+                     Path.LINETO,
+                     Path.LINETO]
+
+            return Path(vertices, codes)
+
+    _style_list["bar"] = Bar
+
+
     __doc__ = cbook.dedent(__doc__) % \
            {"AvailableConnectorstyles": _pprint_styles(_style_list)}
 

Modified: trunk/matplotlib/lib/matplotlib/text.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/text.py     2009-03-19 14:44:01 UTC (rev 
6998)
+++ trunk/matplotlib/lib/matplotlib/text.py     2009-03-20 20:10:42 UTC (rev 
6999)
@@ -406,6 +406,10 @@
             props = props.copy() # don't want to alter the pad externally
             pad = props.pop('pad', 4)
             pad = renderer.points_to_pixels(pad)
+            if self._text == "":
+                self.arrow_patch.set_patchA(None)
+                return
+            
             bbox = self.get_window_extent(renderer)
             l,b,w,h = bbox.bounds
             l-=pad/2.


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to