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记录布局数据后可以复用位置信息。 |
|