当前位置: 首页>>代码示例>>Python>>正文


Python Path.vertices方法代码示例

本文整理汇总了Python中matplotlib.path.Path.vertices方法的典型用法代码示例。如果您正苦于以下问题:Python Path.vertices方法的具体用法?Python Path.vertices怎么用?Python Path.vertices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在matplotlib.path.Path的用法示例。


在下文中一共展示了Path.vertices方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: transform_path

# 需要导入模块: from matplotlib.path import Path [as 别名]
# 或者: from matplotlib.path.Path import vertices [as 别名]
    def transform_path(self, path):
        
        
        def path_interpolation(path, n_steps):
            path_verts, path_codes =  zip(*list(path.iter_segments(curves=False)))
            path_verts = np.array(path_verts)
            path_codes = np.array(path_codes)
            verts_split_inds = np.where(path_codes == Path.MOVETO)[0]
            verts_split = np.split(path_verts, verts_split_inds, 0)
            codes_split = np.split(path_codes, verts_split_inds, 0)
            
            v_collection = []
            c_collection = []
            for path_verts, path_codes in zip(verts_split, codes_split):
                if len(path_verts) == 0:
                    continue
                import matplotlib.cbook
#                print path_verts.shape
                verts = matplotlib.cbook.simple_linear_interpolation(path_verts, n_steps)
                v_collection.append(verts)
#                print verts.shape
                codes = np.ones(verts.shape[0]) * Path.LINETO
                codes[0] = Path.MOVETO
                c_collection.append(codes)
                
            return Path(np.concatenate(v_collection), np.concatenate(c_collection))
            
#        print 'transform path:', path
#        print 'p shape: ', path.vertices, path.vertices.shape
        if path.vertices.shape == (1, 2):
            return Path(self.transform_no_mod(path.vertices))
        
        
#        print '\n'.join(['%s %s' % (point, code) for point, code in path.iter_segments()])
        
#        p2 = polygon.Polygon(((-180, -90), (180, -90), (180, 90), (-180, 90)))
        p2 = self.projection.edge
        p2 = path_interpolation(p2, 30)
        p2 = shape_convert.path_to_geos(p2)
        p2 = p2[0]
        
        paths = []
        for shp in shape_convert.path_to_geos(path):
            shps = []
            for lon in range(-360, 720, 360):
#            for lon in range(0, 360, 360):
                if isinstance(shp, polygon.Polygon):
                    c_shp = polygon.Polygon(np.array(shp.exterior) - [lon, 0], 
                                            [np.array(ring) - [lon, 0] for ring in shp.interiors])
                elif isinstance(shp, linestring.LineString):
                    c_shp = linestring.LineString(np.array(shp) - [lon, 0])
                elif isinstance(shp, MultiLineString):
                    c_shp = MultiLineString([linestring.LineString(np.array(s) - [lon, 0]) for s in shp.geoms])
                else:
                    raise ValueError('Unknown shapely object (%s).' % (type(shp)))
                
                shps.append(c_shp)
            
            # Turn the list of shapes into a Geos type    
            if isinstance(shp, polygon.Polygon):
                shps = MultiPolygon(shps)
            elif isinstance(shp, linestring.LineString):
                shps = MultiLineString(shps)
            else:
                ValueError('Unknown shape type')
            
            # join the shapes back together again if they have wrapped the entire 360 range.
            shps = cascaded_union(shps)        
            
            # Do one more intersection of the overall union (this seems to be necessary)
            intersection_shp = shps.intersection(p2)
            pths = shape_convert.geos_to_path(intersection_shp)

#            pths = shape_convert.geos_to_path(shps)
            
            # Now interpolate the paths 
            interp_resolution = 15
#            interp_resolution = 5
    
            
#            for pth in pths:
#                p9 = path_interpolation(pth, 9)
##                print '\n------' * 30
##                print 'interp 9:', p9
##                print '\n------' * 5
##                print 'interp 5:', path_interpolation(pth, 5)
##                print '\n------' * 30
#                paths.append(path_interpolation(pth, 9))
#            
            paths.extend([path_interpolation(pth, interp_resolution) for pth in pths])
    
    
        if len(paths) == 1:
            path = paths[0]
        elif len(paths) == 0:
            return Path(np.empty([0,2]))
        else:
            points = []
            codes = []
            for path in paths:
#.........这里部分代码省略.........
开发者ID:pelson,项目名称:mpl_mapping_trial,代码行数:103,代码来源:custom_projection.py

示例2: transform_path

# 需要导入模块: from matplotlib.path import Path [as 别名]
# 或者: from matplotlib.path.Path import vertices [as 别名]
    def transform_path(self, path):
        
        
        def path_interpolation(path, n_steps):
            path_verts, path_codes =  zip(*list(path.iter_segments(curves=False)))
            path_verts = np.array(path_verts)
            path_codes = np.array(path_codes)
            verts_split_inds = np.where(path_codes == Path.MOVETO)[0]
            verts_split = np.split(path_verts, verts_split_inds, 0)
            codes_split = np.split(path_codes, verts_split_inds, 0)
            
            v_collection = []
            c_collection = []
            for path_verts, path_codes in zip(verts_split, codes_split):
                if len(path_verts) == 0:
                    continue
                import matplotlib.cbook
#                print path_verts.shape
                verts = matplotlib.cbook.simple_linear_interpolation(path_verts, n_steps)
                v_collection.append(verts)
#                print verts.shape
                codes = np.ones(verts.shape[0]) * Path.LINETO
                codes[0] = Path.MOVETO
                c_collection.append(codes)
                
            return Path(np.concatenate(v_collection), np.concatenate(c_collection))
            
#        print 'transform path:', path
#        print 'p shape: ', path.vertices, path.vertices.shape
        if path.vertices.shape == (1, 2):
            return Path(self.transform_no_mod(path.vertices))
        
        
#        print '\n'.join(['%s %s' % (point, code) for point, code in path.iter_segments()])
        
        p2 = polygon.Polygon(((-180, -90), (180, -90), (180, 90), (-180, 90)))
        
        paths = []
        shps = []
        for shp in shape_convert.path_to_geos(path):
            shps = []
            for lon in range(-360, 720, 360):
#            for lon in range(0, 360, 360):
                if isinstance(shp, polygon.Polygon):
                    c_shp = polygon.Polygon(np.array(shp.exterior) - [lon, 0], 
                                            [np.array(ring) - [lon, 0] for ring in shp.interiors])
                elif isinstance(shp, linestring.LineString):
                    c_shp = linestring.LineString(np.array(shp) - [lon, 0])
                elif isinstance(shp, MultiLineString):
                    c_shp = MultiLineString([linestring.LineString(np.array(s) - [lon, 0]) for s in shp.geoms])
                else:
                    raise ValueError('Unknown shapely object (%s).' % (type(shp)))
                
                shps.append(c_shp)
                
            if isinstance(shp, polygon.Polygon):
                shps = MultiPolygon(shps)
            elif isinstance(shp, linestring.LineString):
                shps = MultiLineString(shps)
            else:
                ValueError('Unknown shape type')
            
            # join the shapes back together again if they have wrapped the entire 360 range.
            from shapely.ops import cascaded_union
            shps = cascaded_union(shps)        
            interp_resolution = 40
    
#            try:
            intersection_shp = shps.intersection(p2)
            pths = shape_convert.geos_to_path(intersection_shp)
#            print pths
            
            paths.extend([path_interpolation(pth, interp_resolution) for pth in pths])
#            print paths
#            except shapely.geos.TopologicalError:
#                print 'failed with: ', shps
#                print 'orig path: ', path
#                print 'path: ', shape_convert.geos_to_path(shps)
#                import matplotlib.pyplot as plt
#                from matplotlib.collections import PatchCollection
#                import matplotlib.patches as mpatches
#                import matplotlib.cm
#
#                plt.close()
#                pth = shape_convert.geos_to_path(shps)[0]
#                poly = mpatches.PathPatch(pth)
#                collection = PatchCollection([poly], cmap=matplotlib.cm.jet, alpha=0.4)
#                plt.gca().add_collection(collection)
#                plt.show()
    
        if len(paths) == 1:
            path = paths[0]
        elif len(paths) == 0:
            return Path(np.empty([0,2]))
        else:
            points = []
            codes = []
            for path in paths:
                path_points, path_codes = zip(*(path.iter_segments()))
                points.append(path_points)
#.........这里部分代码省略.........
开发者ID:pelson,项目名称:mpl_mapping_trial,代码行数:103,代码来源:mollweide.py


注:本文中的matplotlib.path.Path.vertices方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。