Revision: 7431
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7431&view=rev
Author:   jdh2358
Date:     2009-08-08 15:10:08 +0000 (Sat, 08 Aug 2009)

Log Message:
-----------
use a class helper method to make the compound path from polys

Modified Paths:
--------------
    branches/v0_99_maint/examples/animation/histogram_tkagg.py
    branches/v0_99_maint/examples/api/histogram_path_demo.py
    branches/v0_99_maint/lib/matplotlib/path.py

Modified: branches/v0_99_maint/examples/animation/histogram_tkagg.py
===================================================================
--- branches/v0_99_maint/examples/animation/histogram_tkagg.py  2009-08-08 
13:58:52 UTC (rev 7430)
+++ branches/v0_99_maint/examples/animation/histogram_tkagg.py  2009-08-08 
15:10:08 UTC (rev 7431)
@@ -2,6 +2,7 @@
 This example shows how to use a path patch to draw a bunch of
 rectangles for an animated histogram
 """
+import time
 import numpy as np
 import matplotlib
 matplotlib.use('TkAgg') # do this before importing pylab
@@ -52,6 +53,10 @@
 ax.set_ylim(bottom.min(), top.max())
 
 def animate():
+    if animate.cnt>=100:
+        return
+
+    animate.cnt += 1
     # simulate new data coming in
     data = np.random.randn(1000)
     n, bins = np.histogram(data, 100)
@@ -59,11 +64,7 @@
     verts[1::5,1] = top
     verts[2::5,1] = top
     fig.canvas.draw()
-
-def run():
-    for i in range(100):
-        fig.canvas.manager.window.after(100, animate)
-
-
-fig.canvas.manager.window.after(100, run)
+    fig.canvas.manager.window.after(100, animate)
+animate.cnt = 0
+fig.canvas.manager.window.after(100, animate)
 plt.show()

Modified: branches/v0_99_maint/examples/api/histogram_path_demo.py
===================================================================
--- branches/v0_99_maint/examples/api/histogram_path_demo.py    2009-08-08 
13:58:52 UTC (rev 7430)
+++ branches/v0_99_maint/examples/api/histogram_path_demo.py    2009-08-08 
15:10:08 UTC (rev 7431)
@@ -20,8 +20,9 @@
 
 # histogram our data with numpy
 data = np.random.randn(1000)
-n, bins = np.histogram(data, 100)
+n, bins = np.histogram(data, 50)
 
+
 # get the corners of the rectangles for the histogram
 left = np.array(bins[:-1])
 right = np.array(bins[1:])
@@ -29,28 +30,24 @@
 top = bottom + n
 nrects = len(left)
 
-# here comes the tricky part -- we have to set up the vertex and path
-# codes arrays using moveto, lineto and closepoly
+XY = np.zeros((nrects, 4, 2))
+XY[:,0,0] = left
+XY[:,0,1] = bottom
 
-# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
-# CLOSEPOLY; the vert for the closepoly is ignored but we still need
-# it to keep the codes aligned with the vertices
-nverts = nrects*(1+3+1)
-verts = np.zeros((nverts, 2))
-codes = np.ones(nverts, int) * path.Path.LINETO
-codes[0::5] = path.Path.MOVETO
-codes[4::5] = path.Path.CLOSEPOLY
-verts[0::5,0] = left
-verts[0::5,1] = bottom
-verts[1::5,0] = left
-verts[1::5,1] = top
-verts[2::5,0] = right
-verts[2::5,1] = top
-verts[3::5,0] = right
-verts[3::5,1] = bottom
+XY[:,1,0] = left
+XY[:,1,1] = top
 
-barpath = path.Path(verts, codes)
-patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', 
alpha=0.5)
+XY[:,2,0] = right
+XY[:,2,1] = top
+
+XY[:,3,0] = right
+XY[:,3,1] = bottom
+
+
+
+barpath = path.Path.make_compound_path_from_polys(XY)
+print barpath.codes[:7], barpath.codes[-7:]
+patch = patches.PathPatch(barpath, facecolor='blue', edgecolor='gray', 
alpha=0.8)
 ax.add_patch(patch)
 
 ax.set_xlim(left[0], right[-1])

Modified: branches/v0_99_maint/lib/matplotlib/path.py
===================================================================
--- branches/v0_99_maint/lib/matplotlib/path.py 2009-08-08 13:58:52 UTC (rev 
7430)
+++ branches/v0_99_maint/lib/matplotlib/path.py 2009-08-08 15:10:08 UTC (rev 
7431)
@@ -129,6 +129,34 @@
         self._interpolation_steps = _interpolation_steps
 
     @classmethod
+    def make_compound_path_from_polys(cls, XY):
+        """
+        (static method) Make a compound path object to draw a number
+        of polygons with equal numbers of sides XY is a (numpolys x
+        numsides x 2) numpy array of vertices.  Return object is a
+        :class:`Path`
+
+        .. plot:: mpl_examples/api/histogram_path_demo.py
+
+        """
+
+        # for each poly: 1 for the MOVETO, (numsides-1) for the LINETO, 1 for 
the
+        # CLOSEPOLY; the vert for the closepoly is ignored but we still need
+        # it to keep the codes aligned with the vertices
+        numpolys, numsides, two = XY.shape
+        assert(two==2)
+        stride = numsides + 1
+        nverts = numpolys * stride
+        verts = np.zeros((nverts, 2))
+        codes = np.ones(nverts, int) * cls.LINETO
+        codes[0::stride] = cls.MOVETO
+        codes[numsides::stride] = cls.CLOSEPOLY
+        for i in range(numsides):
+            verts[i::stride] = XY[:,i]
+
+        return cls(verts, codes)
+
+    @classmethod
     def make_compound_path(cls, *args):
         """
         (staticmethod) Make a compound path from a list of Path


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

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to