使用pagerunk对全球机场进行排序
import networkx as nx flights_network = nx.read_edgelist("./input/out.opsahl-openflights.csv",create_using=nx.DiGraph()) print("航班数:" + str(len(flights_network.nodes))) print("航线数:" + str(len(flights_network.edges))) import matplotlib.pyplot as plt %matplotlib inline fig, ax = plt.subplots(figsize=(24, 16)) pos_flights = nx.kamada_kawai_layout(flights_network) #网络布局 ax.axis("off") plt.box(False) nx.draw(flights_network, node_size=30,node_color = "green", edge_color = "#D8D8D8",width=.3, ax=ax) largest_component = max(nx.weakly_connected_component_subgraphs(flights_network), key=len)#找出最大连通子图 print("航班数:" + str(len(largest_component.nodes))) print("航线数:" + str(len(largest_component.edges))) fig, ax = plt.subplots(figsize=(24, 16)) pos_flights2 = nx.kamada_kawai_layout(largest_component) ax.axis("off") plt.box(False) nx.draw(largest_component, node_size=30,node_color = "green", edge_color = "#D8D8D8",width=.3,pos = pos_flights2, ax=ax) pr_dict = nx.pagerank(largest_component) import pandas as pd pr_df = pd.DataFrame.from_dict(pr_dict,orient="index") pr_df.columns = ["pr_value"] pr_df.sort_values(by = "pr_value").head(20) pr_df.head(20) def get_nodesize_pagerank(pagerank, min_size, max_size): nodesize_list = [] pr_max = max(pagerank.values()) for node, pr in pagerank.items(): nodesize = (max_size - min_size)*pr/pr_max + min_size nodesize_list.append(nodesize) return nodesize_list fig, ax = plt.subplots(figsize=(24, 16)) pos_flights2 = nx.kamada_kawai_layout(largest_component) ax.axis("off") plt.box(False) nx.draw(largest_component, node_size=get_nodesize_pagerank(pr_dict,1,100),node_color = "green", edge_color = "#D8D8D8",width=.3,pos = pos_flights2, ax=ax)