def display_graph(self): """ Transform the graph to a `networkx.DiGraph`-structure and display it using `matplotlib` -- if the necessary libraries are installed. :return: `True` if the graph was drawn, `False` otherwise. """ G = self.create_network_graph() if nx and plt and G: try: from networkx.drawing.nx_agraph import graphviz_layout pos = graphviz_layout(G, prog='dot') except ModuleNotFoundError: from networkx.drawing.layout import shell_layout pos = shell_layout(G) except ImportError: from networkx.drawing.layout import shell_layout pos = shell_layout(G) plt.subplot(111) plt.axis('off') nx.draw_networkx_nodes(G, pos, node_color='r', node_size=1250, nodelist=[ v.display_name for v in self.vertices if v.is_sampled ]) nx.draw_networkx_nodes(G, pos, node_color='b', node_size=1250, nodelist=[ v.display_name for v in self.vertices if v.is_observed ]) for v in self.vertices: nx.draw_networkx_edges(G, pos, arrows=True, edgelist=[(a.display_name, v.display_name) for a in v.dist_ancestors]) nx.draw_networkx_edges(G, pos, arrows=True, style='dashed', edge_color='g', edgelist=[(a.display_name, v.display_name) for a in v.cond_ancestors]) nx.draw_networkx_labels(G, pos, font_color='w', font_weight='bold') plt.show() return True else: return False
def plot(part): n = makenode(part) proc = n.graph() G = nx.Graph() G.add_edges_from(proc) # TODO: can this be done without reading and writing a file? nx.write_edgelist(G, path="grid.edgelist", delimiter=":") H = nx.read_edgelist(path="grid.edgelist", delimiter=":") os.unlink("grid.edgelist") layout = l.kamada_kawai_layout(G, pos=l.shell_layout(G)) nx.draw( H, pos=layout, edge_color="#777777", node_color="#ffffff", with_labels=True, font_family="Arial", font_size=9, label=part, ) #plt.show() if not os.path.isdir('output'): os.makedirs('output') path = os.path.join('output', part + '.svg') plt.savefig(path, format="svg") plt.close()
def draw_shell(G, **kwargs): """Draw networkx graph with shell layout""" from networkx.drawing.layout import shell_layout nlist = kwargs.get('nlist', None) if nlist != None: del (kwargs['nlist']) draw(G, shell_layout(G, nlist=nlist), **kwargs)
def draw_shell(G, **kwargs): """Draw networkx graph with shell layout""" from networkx.drawing.layout import shell_layout nlist = kwargs.get('nlist', None) if nlist != None: del(kwargs['nlist']) draw(G,shell_layout(G,nlist=nlist),**kwargs)
def draw_shell(G, **kwargs): """Draw networkx graph with shell layout. Parameters ---------- G : graph A networkx graph kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. """ nlist = kwargs.get('nlist', None) if nlist is not None: del(kwargs['nlist']) draw(G, shell_layout(G, nlist=nlist), **kwargs)
def draw_shell(G, **kwargs): """Draw networkx graph with shell layout. Parameters ---------- G : graph A networkx graph **kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. """ nlist = kwargs.get('nlist', None) if nlist is not None: del(kwargs['nlist']) draw(G, shell_layout(G, nlist=nlist), **kwargs)
def draw_shell(G, **kwargs): """Draw networkx graph with shell layout.""" nlist = kwargs.get('nlist', None) if nlist != None: del(kwargs['nlist']) draw(G,shell_layout(G,nlist=nlist),**kwargs)
def draw_shell(G, **kwargs): """Draw networkx graph with shell layout.""" nlist = kwargs.get('nlist', None) if nlist is not None: del (kwargs['nlist']) draw(G, shell_layout(G, nlist=nlist), **kwargs)
def display_graph(vertices): """ Transform the graph to a `networkx.DiGraph`-structure and display it using `matplotlib` -- if the necessary libraries are installed. :return: `True` if the graph was drawn, `False` otherwise. """ G = create_network_graph(vertices) _is_conditioned = None if _nx and _plt and G: try: from networkx.drawing.nx_agraph import graphviz_layout pos = graphviz_layout(G, prog='dot') except ModuleNotFoundError: from networkx.drawing.layout import shell_layout pos = shell_layout(G) except ImportError: from networkx.drawing.layout import shell_layout pos = shell_layout(G) _plt.subplot(111) _plt.axis('off') _nx.draw_networkx_nodes( G, pos, node_color='r', node_size=1250, nodelist=[v.display_name for v in vertices if v.is_sampled]) _nx.draw_networkx_nodes( G, pos, node_color='b', node_size=1250, nodelist=[v.display_name for v in vertices if v.is_observed]) for v in vertices: _nx.draw_networkx_edges(G, pos, arrows=True, edgelist=[(a.display_name, v.display_name) for a in v.ancestors]) if v.condition_ancestors is not None and len( v.condition_ancestors) > 0: _is_conditioned = 1 _nx.draw_networkx_edges(G, pos, arrows=True, style='dashed', edge_color='g', edgelist=[ (a.display_name, v.display_name) for a in v.condition_ancestors ]) _nx.draw_networkx_labels(G, pos, font_color='w', font_weight='bold') # for node, _ in G.nodes(): red_patch = mpatches.Circle((0, 0), radius=2, color='r', label='Sampled Variables') blue_patch = mpatches.Circle((0, 0), radius=2, color='b', label='Observed Variables') green_patch = mpatches.Circle( (0, 0), radius=2, color='g', label='Conditioned Variables') if _is_conditioned else 0 if _is_conditioned: _plt.legend(handles=[red_patch, blue_patch, green_patch]) else: _plt.legend(handles=[red_patch, blue_patch]) _plt.show() return True else: return False