我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用networkx.draw_spectral()。
def plot_graph(self, file_name: str='graph.png', label_nodes: bool=True, label_edges: bool=True): import matplotlib.pyplot as plt # pos = nx.spring_layout(self.graph) pos = nx.shell_layout(self.graph, dim=1024, scale=0.5) # pos = nx.random_layout(self.graph, dim=1024, scale=0.5) if label_edges: edge_labels = { (edge[0], edge[1]): edge[2]['object'] for edge in self.graph.edges(data=True) } nx.draw_networkx_edge_labels(self.graph, pos, edge_labels, font_size=5) if label_nodes: labels = {node[0]: node[1] for node in self.graph.nodes(data=True)} nx.draw_networkx_labels(self.graph, pos, labels, font_size=5, alpha=0.8) # nx.draw(self.graph, with_labels=True, arrows=True, node_size=80) nx.draw_spectral(self.graph, with_labels=True, arrows=True, node_size=80) plt.savefig(file_name, dpi=1024)
def draw_graph(G): d = nx.degree(G) nx.draw_spectral(G, nodelist=d.keys(), node_size=[v * 100 for v in d.values()]) plt.show()
def draw_graph_spectral(y, clusters='blue', ns=30): G = nxG(y) pos = graphviz_layout(G, prog='twopi', args='') plt.figure() nx.draw_spectral(G, cmap = plt.get_cmap('jet'), node_color = clusters, node_size=30, with_labels=False)
def plot_ibp(model, target_dir=None, block=False, columns=[0], separate=False, K=4): G = nx.from_numpy_matrix(model.Y(), nx.DiGraph()) F = model.leftordered() W = model._W # Plot Adjacency Matrix draw_adjmat(model._Y) # Plot Log likelihood plot_csv(target_dir=target_dir, columns=columns, separate=separate) #W[np.where(np.logical_and(W>-1.6, W<1.6))] = 0 #W[W <= -1.6]= -1 #W[W >= 1.6] = 1 # KMeans test clusters = kmeans(F, K=K) nodelist_kmeans = [k[0] for k in sorted(zip(range(len(clusters)), clusters), key=lambda k: k[1])] adj_mat_kmeans = nx.adjacency_matrix(G, nodelist=nodelist_kmeans).A draw_adjmat(adj_mat_kmeans, title='KMeans on feature matrix') # Adjacency matrix generation draw_adjmat(model.generate(nodelist_kmeans), title='Generated Y from ILFRM') # training Rescal R = rescal(model._Y, K) R = R[nodelist_kmeans, :][:, nodelist_kmeans] draw_adjmat(R, 'Rescal generated') # Networks Plots f = plt.figure() ax = f.add_subplot(121) title = 'Features matrix, K = %d' % model._K ax.set_title(title) ColorMap(F, pixelspervalue=5, title=title, ax=ax) ax = f.add_subplot(122) ax.set_title('W') img = ax.imshow(W, interpolation='None') plt.colorbar(img) f = plt.figure() ax = f.add_subplot(221) ax.set_title('Spectral') nx.draw_spectral(G, axes=ax) ax = f.add_subplot(222) ax.set_title('Spring') nx.draw(G, axes=ax) ax = f.add_subplot(223) ax.set_title('Random') nx.draw_random(G, axes=ax) ax = f.add_subplot(224) ax.set_title('graphviz') try: nx.draw_graphviz(G, axes=ax) except: pass display(block=block)