def draw(self): nx.draw( self.graph, pos=pydot_layout(self.graph), # label='gnp_random_graph({N},{p})', with_labels=True) return self
def drawGraph(gToDraw): plt.figure(figsize=(len(gToDraw.edges()) * 2, len(gToDraw.edges()) * 2)) plt.clf() # pos = graphviz_layout(gToDraw) pos = pydot_layout(gToDraw) try: dNodeLabels = {} # For each node for nCurNode in gToDraw.nodes(): # Try to add weight dNodeLabels[nCurNode] = "%s (%4.2f)" % ( str(nCurNode), gToDraw.node[nCurNode]['weight']) except KeyError: # Weights could not be added, use nodes as usual dNodeLabels = None nx.draw_networkx(gToDraw, pos, arrows=False, node_size=1200, color="blue", with_labels=True, labels=dNodeLabels) labels = nx.get_edge_attributes(gToDraw, 'weight') nx.draw_networkx_edge_labels(gToDraw, pos, edge_labels=labels) plt.show()
def _draw_dag(dag, config): fig, ax = plt.subplots(figsize=(16, 12)) fig.suptitle("Task Graph", fontsize=24) # Relabel absolute paths to path names. project_directory = Path(config["project_directory"]) mapping = { node: Path(node).relative_to(project_directory) for node in dag.nodes if Path(node).is_absolute() } dag = nx.relabel_nodes(dag, mapping) layout = nx_pydot.pydot_layout(dag, prog="dot") nx.draw_networkx_edges(dag, pos=layout, ax=ax) nx.draw_networkx_labels(dag, pos=layout, ax=ax) # Draw non-task nodes. non_task_nodes = [ node for node in dag.nodes if not dag.nodes[node]["_is_task"] ] nx.draw_networkx_nodes(dag, pos=layout, nodelist=non_task_nodes, node_color=BLUE, ax=ax) task_nodes = [node for node in dag.nodes if dag.nodes[node]["_is_task"]] if config["priority_scheduling"]: node_size = np.array( [dag.nodes[node]["priority"] for node in task_nodes]) node_size_demeaned = node_size - node_size.min() node_size_relative = node_size_demeaned / node_size_demeaned.max() node_size = node_size_relative * 1_000 + 300 cmap = LinearSegmentedColormap.from_list("cmap", YELLOW_TO_RED) priority_kwargs = { "node_size": node_size, "node_color": node_size_relative, "cmap": cmap, } else: priority_kwargs = {"node_color": BLUE} im = nx.draw_networkx_nodes(dag, pos=layout, nodelist=task_nodes, **priority_kwargs, ax=ax) if config["priority_scheduling"]: divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="3%", pad=0.1) fig.colorbar(im, cax=cax, orientation="vertical") cax.set_title("Priority") path = Path(config["hidden_build_directory"], ".dag.png") path.parent.mkdir(parents=True, exist_ok=True) plt.savefig(path) plt.close()
def build_visualization_positions(network): """ Builds the positional spacing of all nodes(markers) based on either pydot if available or falling back to a python computation :param network: :return: position array """ try: # First try to use pydot and dot, as it produces the most aesthetically pleasing trees from networkx.drawing.nx_pydot import pydot_layout return pydot_layout(network, prog="dot") except: # Fall back to python function so we don't have to require users to install graphviz return hierarchy_pos(network, width=2.0, leaf_vs_root_factor=1.0)
def communities(SG, outfile, title, plotColors): pos = pydot_layout(SG) # pos = nx.random_layout(SG) # pos = graphviz_layout(SG, prog='dot') # pos = graphviz_layout(SG) # pos = nx.shell_layout(SG) # pos = nx.spring_layout(SG) # pos = nx.spectral_layout(SG) partition = community.best_partition(SG) communities = set(partition.values()) communityEdges = [] for com in communities: list_nodes = [ nodes for nodes in partition.keys() if partition[nodes] == com ] communityGraph = SG.subgraph(list_nodes) communityColor = plotColors[com] communityEdges += communityGraph.edges() nx.draw_networkx_edges(communityGraph, pos, alpha=0.25, edge_color=communityColor) nx.draw_networkx_nodes(communityGraph, pos, node_size=20, alpha=0) nx.draw_networkx_labels(communityGraph, pos, font_size=12, font_color=communityColor, font_family='sans-serif') communityEdges = [sorted(e) for e in communityEdges] nonCommunityEdges = [sorted(e) for e in SG.edges()] nonCommunityEdges = [ e for e in nonCommunityEdges if e not in communityEdges ] nx.draw_networkx_edges(SG, pos, edgelist=nonCommunityEdges, edge_color="gray", alpha=0.3, style="dashed") plt.axis('off') plt.title(title) plt.savefig(outfile, dpi=500) plt.close()
def main(args): # create a random graph # G=nx.gnp_random_graph(n=10,p=0.6) G = read_dot(args.dot_file) # remember the coordinates of the vertices if args.layout == "shell": coords = nx.shell_layout(G) else: coords = pydot_layout(G) # remove "len(clique)>2" if you're interested in maxcliques with 2 edges cliques = [clique for clique in nx.find_cliques(G) if len(clique) > 2] # #draw the graph nx.draw(G, pos=coords, node_size=200) print(coords) in_more_than_one_clique = set() seen = set() for clique in cliques: for node in clique: if node not in seen: seen.add(node) else: in_more_than_one_clique.add(node) # already_processed_nodes = set() for clique in cliques: print("Clique to appear: ", clique) color = draw_circle_around_clique(clique, coords) node_colors = [] for node in clique: if node not in in_more_than_one_clique: node_colors.append(color) else: node_colors.append("grey") nx.draw_networkx_nodes(G, pos=coords, nodelist=clique, node_color=node_colors, node_size=200) # already_processed_nodes.update(clique) nx.draw_networkx_labels(G, coords, font_size=8) # nx.draw_networkx_nodes(G,pos=coords, nodelist=clique) plt.savefig(args.outfile) plt.close()
def plot(SG, outfile, weight_cutoffs, title): edges1 = [(u, v) for (u, v, d) in SG.edges(data=True) if d['weight'] <= weight_cutoffs[1]] edges2 = [ (u, v) for (u, v, d) in SG.edges(data=True) if d['weight'] > weight_cutoffs[1] and d['weight'] <= weight_cutoffs[2] ] edges3 = [(u, v) for (u, v, d) in SG.edges(data=True) if d['weight'] > weight_cutoffs[2]] pos = pydot_layout(SG) # edges nx.draw_networkx_edges(SG, pos, edgelist=edges1, width=2, edge_color="yellow", alpha=0.7) nx.draw_networkx_edges(SG, pos, edgelist=edges2, width=2, edge_color="orange", alpha=0.7) nx.draw_networkx_edges(SG, pos, edgelist=edges3, width=2, edge_color="red", alpha=0.8) # labels nx.draw_networkx_nodes(SG, pos, node_color="gray", alpha=0.5, node_size=1) nx.draw_networkx_labels(SG, pos, font_size=12, font_family='sans-serif') plt.axis('off') plt.title(title) plt.savefig(outfile) plt.close()
def _layout(graph, **kwargs): return pydot_layout(graph, prog='dot', **kwargs)
def draw_states(self): G = nx.DiGraph(sbn2sbs(self.tpm)) mapping = dict(zip(range(len(self.tpm.index)), self.tpm.index)) S = nx.relabel_nodes(G, mapping) nx.draw(S, pos=pydot_layout(S), with_labels=True)
def _layout(graph, **kwargs): return pydot_layout(graph, prog="dot", **kwargs)