def plot(): fig = plt.figure() axis = fig.add_subplot(1, 1, 1) col = PolyCollection( [], facecolors="none", edgecolors="red", linestyle="--", linewidth=1.2 ) col.set_zorder(1) axis.add_collection(col) axis.set_xlim(0.5, 2.5) axis.set_ylim(0.5, 2.5) axis.collections[0].set_verts([[[1, 1], [1, 2], [2, 2], [2, 1]]]) return fig
def _init_axis_2d(axis, info): axis.set_xlabel(info['labels'][0]) axis.set_ylabel(info['labels'][1]) if 'xlim' in info and info['xlim'] is not None: axis.set_xlim(info['xlim'][0], info['xlim'][1]) if 'ylim' in info and info['ylim'] is not None: axis.set_ylim(info['ylim'][0], info['ylim'][1]) if 'lines' in info: for line in info['lines']: axis.plot([], [], zorder=0, **line)[0] if 'surfaces' in info: for surf in info['surfaces']: col = PolyCollection([], **surf) col.set_zorder(1) axis.add_collection(col)
def plot(): fig = plt.figure() axis = fig.add_subplot(1, 1, 1) col = PolyCollection([], facecolors="none", edgecolors="red", linestyle="--", linewidth=1.2) col.set_zorder(1) axis.add_collection(col) axis.set_xlim(0.5, 2.5) axis.set_ylim(0.5, 2.5) axis.collections[0].set_verts([[[1, 1], [1, 2], [2, 2], [2, 1]]]) return fig
def draw_nx_tapered_edges(G, pos, edgelist=None, width=0.5, edge_color='k', style='solid', alpha=1.0, edge_cmap=None, edge_vmin=None, edge_vmax=None, ax=None, label=None, highlight=None, tapered=False, **kwds): """Draw the edges of the graph G. This draws only the edges of the graph G. Parameters ---------- G : graph A networkx graph pos : dictionary A dictionary with nodes as keys and positions as values. Positions should be sequences of length 2. edgelist : collection of edge tuples Draw only specified edges(default=G.edges()) width : float, or array of floats Line width of edges (default=1.0) edge_color : color string, or array of floats Edge color. Can be a single color format string (default='r'), or a sequence of colors with the same length as edgelist. If numeric values are specified they will be mapped to colors using the edge_cmap and edge_vmin,edge_vmax parameters. style : string Edge line style (default='solid') (solid|dashed|dotted,dashdot) alpha : float The edge transparency (default=1.0) edge_ cmap : Matplotlib colormap Colormap for mapping intensities of edges (default=None) edge_vmin,edge_vmax : floats Minimum and maximum for edge colormap scaling (default=None) ax : Matplotlib Axes object, optional Draw the graph in the specified Matplotlib axes. label : [None| string] Label for legend Returns ------- matplotlib.collection.LineCollection `LineCollection` of the edges Examples -------- >>> G=nx.dodecahedral_graph() >>> edges=nx.draw_networkx_edges(G,pos=nx.spring_layout(G)) Also see the NetworkX drawing examples at http://networkx.github.io/documentation/latest/gallery.html See Also -------- draw() draw_networkx() draw_networkx_nodes() draw_networkx_labels() draw_networkx_edge_labels() """ if ax is None: ax = plt.gca() if edgelist is None: edgelist = list(G.edges()) if not edgelist or len(edgelist) == 0: # no edges! return None if highlight is not None and (isinstance(edge_color, basestring) or not cb.iterable(edge_color)): idMap = {} nodes = G.nodes() for i in range(len(nodes)): idMap[nodes[i]] = i ecol = [edge_color] * len(edgelist) eHighlight = [ highlight[idMap[edge[0]]] or highlight[idMap[edge[1]]] for edge in edgelist ] for i in range(len(eHighlight)): if eHighlight[i]: ecol[i] = '0.0' edge_color = ecol # set edge positions if not cb.iterable(width): lw = np.full(len(edgelist), width) else: lw = width edge_pos = [] wdScale = 0.01 for i in range(len(edgelist)): e = edgelist[i] w = wdScale * lw[i] / 2 p0 = pos[e[0]] p1 = pos[e[1]] dx = p1[0] - p0[0] dy = p1[1] - p0[1] l = math.sqrt(dx * dx + dy * dy) edge_pos.append( ((p0[0] + w * dy / l, p0[1] - w * dx / l), (p0[0] - w * dy / l, p0[1] + w * dx / l), (p1[0], p1[1]))) edge_vertices = np.asarray(edge_pos) if not isinstance(edge_color, basestring) \ and cb.iterable(edge_color) \ and len(edge_color) == len(edge_vertices): if np.alltrue([isinstance(c, basestring) for c in edge_color]): # (should check ALL elements) # list of color letters such as ['k','r','k',...] edge_colors = tuple( [colorConverter.to_rgba(c, alpha) for c in edge_color]) elif np.alltrue([not isinstance(c, basestring) for c in edge_color]): # If color specs are given as (rgb) or (rgba) tuples, we're OK if np.alltrue( [cb.iterable(c) and len(c) in (3, 4) for c in edge_color]): edge_colors = tuple(edge_color) else: # numbers (which are going to be mapped with a colormap) edge_colors = None else: raise ValueError( 'edge_color must consist of either color names or numbers') else: if isinstance(edge_color, basestring) or len(edge_color) == 1: edge_colors = (colorConverter.to_rgba(edge_color, alpha), ) else: raise ValueError( 'edge_color must be a single color or list of exactly m colors where m is the number or edges' ) if tapered: edge_collection = PolyCollection( edge_vertices, facecolors=edge_colors, linewidths=0, antialiaseds=(1, ), transOffset=ax.transData, ) else: edge_collection = LineCollection( edge_pos, colors=edge_colors, linewidths=lw, antialiaseds=(1, ), linestyle=style, transOffset=ax.transData, ) edge_collection.set_zorder(1) # edges go behind nodes edge_collection.set_label(label) ax.add_collection(edge_collection) # Note: there was a bug in mpl regarding the handling of alpha values for # each line in a LineCollection. It was fixed in matplotlib in r7184 and # r7189 (June 6 2009). We should then not set the alpha value globally, # since the user can instead provide per-edge alphas now. Only set it # globally if provided as a scalar. if cb.is_numlike(alpha): edge_collection.set_alpha(alpha) if edge_colors is None: if edge_cmap is not None: assert (isinstance(edge_cmap, Colormap)) edge_collection.set_array(np.asarray(edge_color)) edge_collection.set_cmap(edge_cmap) if edge_vmin is not None or edge_vmax is not None: edge_collection.set_clim(edge_vmin, edge_vmax) else: edge_collection.autoscale() # update view minx = np.amin(np.ravel(edge_vertices[:, :, 0])) maxx = np.amax(np.ravel(edge_vertices[:, :, 0])) miny = np.amin(np.ravel(edge_vertices[:, :, 1])) maxy = np.amax(np.ravel(edge_vertices[:, :, 1])) w = maxx - minx h = maxy - miny padx, pady = 0.05 * w, 0.05 * h corners = (minx - padx, miny - pady), (maxx + padx, maxy + pady) ax.update_datalim(corners) ax.autoscale_view() return edge_collection