def plotPolys(polys, f, title='figure', colors=None): if not len(polys): return fig = plt.figure(f, figsize=(15, 15), dpi=90) fig.canvas.set_window_title(title + str(f)) X = [pnt.x() for poly in polys for pnt in poly.points] Y = [pnt.y() for poly in polys for pnt in poly.points] minX = int(min(X)) maxX = int(max(X)) minY = int(min(Y)) maxY = int(max(Y)) # p1 = Polygon([(0, 0), (0, 2), (2, 2), (2, 0)]) # p2 = Polygon([(1, 0), (1, 2), (3, 2), (3, 0)]) ax = fig.add_subplot(111) if not colors: colors = [[ random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1), 1 ] for poly in polys] for poly, c in zip(polys, colors): ring_patch = PolygonPatch(poly.poly, alpha=c[-1]) ring_patch.set_color(c[:-1]) ax.add_patch(ring_patch) ax.set_title('General Polygon') xrange = [minX - 4, maxX + 4] yrange = [minY - 4, maxY + 4] ax.set_xlim(*xrange) ax.set_xticks(range(*xrange) + [xrange[-1]]) ax.set_ylim(*yrange) ax.set_yticks(range(*yrange) + [yrange[-1]]) ax.set_aspect(1)
def poly_draw(final_polys, canvas=[], fig=[], ax=[], mirror_ax=None, origin=np.zeros(2), cmap=cm.rainbow): from shapely.affinity import scale, translate if not fig: fig, ax = plt.subplots() elecs = list(final_polys.keys()) for num, elec in final_polys.items(): for poly in elec: if poly: if canvas: poly = poly.difference( poly.difference(gem_poly().get_poly( 'box', np.array([0, 0, canvas[0], canvas[1]])))) if mirror_ax: if mirror_ax.lower() == 'y': Q1 = scale(poly, yfact=-1, origin=(0, 0)) elif mirror_ax.lower() == 'x': Q1 = scale(poly, xfact=-1, origin=(0, 0)) Q1 = translate(Q1, -origin[0], -origin[1]) tpatch = PolygonPatch(Q1) tpatch.set_color(cmap(num / max(elecs))) tpatch.set_linewidth(0) ax.add_patch(tpatch) poly = translate(poly, -origin[0], -origin[1]) patch = PolygonPatch(poly) patch.set_color(cmap(num / max(elecs))) patch.set_linewidth(0) ax.add_patch(patch) ax.autoscale(enable=True) ax.set_aspect('equal') return (fig, ax)
def show(self): if self.fig is None: plt.ion() self.fig = plt.figure() self.ax = self.fig.add_subplot(111) self.ax.axis([0,self._width,0,self._height]) plt.show() plt.cla() rob = PolygonPatch(self._robot.get_bounding_box()) rob.set_color("b") self.ax.add_patch(rob) # rob = Polygon([[self._robot.x+.5, self._robot.y+1], [self._robot.x+.5, self._robot.y+1.1], # [self._robot.x-.5, self._robot.y+1.1], [self._robot.x-.5, self._robot.y+1]]) # rob = affinity.rotate(rob, self._robot.rotation, origin=(self._robot.x, self._robot.y), use_radians=True) # rob = PolygonPatch(rob) # rob.set_color("y") # self.ax.add_patch(rob) for i in range(self._red_pixel_sensors): rotation = self._robot.rotation - self._fov / 2 + i * self._fov / (self._red_pixel_sensors-1) ray = affinity.rotate( LineString([ np.array(self._robot.pos.coords)[0], np.array(self._robot.pos.coords)[0] + (self._height*2, 0)]), rotation, origin=self._robot.pos, use_radians=True) patch = PolygonPatch(ray.buffer(0.1)) patch.set_color("r") self.ax.add_patch(patch) for cube in self._red_cubes: patch = PolygonPatch(cube) patch.set_color("r") self.ax.add_patch(patch) for obstacle in self._obstacles: patch = PolygonPatch(obstacle) patch.set_color("b") self.ax.add_patch(patch) # plt.show() # self.fig.savefig() plt.pause(0.01)
# Polygonisation m = MultiLineString(n) triangles = list(polygonize(m)) # Triangles unification a = unary_union(triangles) # Single polygon test if a.geom_type == "Polygon": # Merge triangles shape = cascaded_union(triangles) # Generate Figure patch = PolygonPatch(shape) if condition == '1': patch.set_color( to_rgba('red', alpha=0.6)) elif condition == '2': patch.set_color( to_rgba('orange', alpha=1)) ax.add_patch(patch) break else: # If not a single polygon continue # Extract rest position elif condition == '3': xaverage = 0 yaverage = 0 for p in points: xaverage = xaverage + p[0]
dend_aligned2fixed = np.dot(R, (dend - om - cm).T).T + t + of + cf plt.plot(dend_aligned2fixed[:, dim1], -dend_aligned2fixed[:, dim2], color=stack_to_color[stack], linewidth=0.5) for c in range(0, len(C['CellBody_L'])): cell = C['CellBody_L'][c] cell_closed = vstack((cell, cell[0])) cell_aligned2fixed = np.dot(R, (cell_closed - om - cm).T).T + t + of + cf ring_mixed = Polygon( zip(cell_aligned2fixed[:, dim1], -cell_aligned2fixed[:, dim2])) ring_patch = PolygonPatch(ring_mixed) ring_patch.set_color(stack_to_color[stack]) ax.add_patch(ring_patch) ''' for c in range(0,len(C['7N_L'])): cell = C['7N_L'][c] cell_closed = vstack((cell,cell[0])) cell_aligned2fixed = np.dot(R, (cell_closed - om - cm).T).T + t + of + cf ring_mixed = Polygon(zip(cell_aligned2fixed[:,dim1],cell_aligned2fixed[:,dim2])) ring_patch = PolygonPatch(ring_mixed) ax.add_patch(ring_patch) ''' ax.set_aspect('equal') fp = DataManager.get_mesh_filepath(stack_m='Rat_brainstem_atlas',