Python networkx 模块,random_layout() 实例源码

我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用networkx.random_layout()

项目:GOApy    作者:leopepe    | 项目源码 | 文件源码
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)
项目:web_explorer    作者:nobriot    | 项目源码 | 文件源码
def create_web_network_graph(self):
        ''' Functions that creates a NetworkX network visualization from the 
        explored pages 
        For documentation about NetworkX, check : https://networkx.github.io/'''
        #Create a directed graph
        web_graph=nx.DiGraph()
        # Add our start nodes first to the graph, as the center.
        web_graph.add_nodes_from(self.to_visit_urls[0])

        #Now we explore our results to add the relevant websites to the graph
        for base_url in os.listdir(self.main_directory+"web_content/"):
            if self.is_danish_company(base_url): #Only Danish companies are added : 
                web_graph.add_node(base_url)

        #Explore again to fill up all the edges (connections/links) between websites
        for base_url in os.listdir(self.main_directory+"web_content/"):
            if self.is_danish_company(base_url): # Same as before only Danish companies
                #Load up the links from this Danish company to other websites
                filename = self.main_directory+"web_content/"+base_url+"/external_urls_"+str(self.redirect_count)+"_redirect.p"
                external_base_urls=pickle.load(open(filename, "rb" ))

                #Now we also filter the list of external links
                for external_link in external_base_urls:
                    if web_graph.has_node(external_link) : # The link is also in the graph, so the connection is added
                        web_graph.add_edge(base_url,external_link)

        #Finally draw the network 
        #plt.figure(figsize=(120, 90))
        plt.figure(figsize=(40, 40))
        pos = nx.random_layout(web_graph)
        nx.draw_networkx_nodes(web_graph,pos,node_size=2500)
        nx.draw_networkx_nodes(web_graph,nodelist=self.to_visit_urls[0],pos=pos,node_size=3000,node_color='b')
        #nx.draw_networkx_labels(web_graph,pos,fontsize=12)
        nx.draw_networkx_edges(web_graph,pos,alpha=0.5)
        plt.savefig(self.main_directory+"DTU network.png",dpi=40)
        plt.show()
项目:bayesianpy    作者:morganics    | 项目源码 | 文件源码
def random_layout(self, graph):
        return nx.random_layout(graph,center=[0.5,0.5])
项目:analyse_website_dns    作者:mrcheng0910    | 项目源码 | 文件源码
def main():
    """
    ?????
    :return:
    """
    edges = []   # ???????
    domain_name = 'jd.com'
    domain_pkts = get_data(domain_name)
    from collections import defaultdict
    node_dict = defaultdict(set)
    for i in domain_pkts[0]['details']:
        for v in i['answers']:
            if v['dm_type'] == 'CNAME':
                node_dict[v['domain_name']].add('main')
                node_dict[v['dm_data']].add('cname')
            else:
                node_dict[v['dm_data']].add('ip')
                node_dict[v['domain_name']].add('main')
            edges.append((v['domain_name'],v['dm_data']))
    node_cat ={}
    for i in node_dict:
        if 'ip' in list(node_dict[i]):
            node_cat[i]='ip'
        elif 'cname' in list(node_dict[i]):
            node_cat[i]='cname'
        else:
            node_cat[i]='main'

    plt.figure(1,figsize=(10,8))
    G=nx.Graph()
    G.add_edges_from(edges)

    for node in G.nodes():
        G.node[node]['category'] = node_cat[node]

    color_map = {'main': 'b', 'cname': 'c', 'ip': 'r'}
    pos = graphviz_layout(G, prog="neato")  # neato fdp
    # pos = nx.random_layout(G)
    nx.draw(G,
            pos,
            node_size=100,
            node_color=[color_map[G.node[node]['category']] for node in G],
            label="nihao"
            )
    plt.axis('off')
    plt.savefig('./graph/' + domain_name + "_type.png", dpi=75)
    plt.show()