1. 先打基礎
1 | 基礎學習筆記 |
Graph CreationNetworkX graph objects can be created in one of three ways:
Edge attributes can be anything: import math
G.add_edge('y', 'x', function=math.cos)
G.add_node(math.cos) # any hashable can be a node
已有生成演算法生成專門的圖 Graph generators such as
從檔案讀入圖資料-寫圖資料到外部檔案 For importing network data from formats such as GML, GraphML, edge list text files see the reading and writing graphs subpackage.
|
|
Graph Reporting 和 Algorithms |
|
Drawing1 import matplotlib.pyplot as plt 2 G = nx.cubical_graph() 3 subax1 = plt.subplot(121) 4 nx.draw(G) # default spring_layout 5 subax2 = plt.subplot(122) 6 nx.draw(G, pos=nx.circular_layout(G), node_color='r', edge_color='b')
|
|
Data StructureNetworkX uses a “dictionary of dictionaries of dictionaries” as the basic network data structure. This allows fast lookup with reasonable storage for large sparse networks. The keys are nodes so
Advantages of dict-of-dicts-of-dicts data structure:
|
|
ego_graph返回給定半徑內以節點n為中心的鄰居的誘導子圖。 |
|
2.關於佈局
1. 記錄已有的佈局資料——重複繪圖時使用上次的佈局 舉例: draw_planar——用平面佈局畫一個平面網路圖G。
This is a convenience function equivalent to: nx.draw(G, pos=nx.planar_layout(G), **kwargs)
每次呼叫該函式時都會計算佈局。對於重複繪製,直接呼叫planar_layout並重用結果會更有效: 1 G = nx.path_graph(5) 2 pos = nx.planar_layout(G) 3 nx.draw(G, pos=pos) # Draw the original graph 4 # Draw a subgraph, reusing the same node positions 5 nx.draw(G.subgraph([0, 1, 2]), pos=pos, node_color="red") 使用pos記錄佈局資料後可以複用位置資訊。 |
|