NetworkX安装与内置图
验证安装成功
import networkx as nx
nx.__version__
'3.0'
设置matplotlib中文
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rc("font",family="SimHei")
plt.rcParams['axes.unicode_minus']=False
plt.plot([1,2,3],[100,200,300])
plt.title("中文")
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.show()

经典图结构
全连接无向图
G=nx.complete_graph(7)
nx.draw(G)

G.size()
21
全连接有向图
G=nx.complete_graph(7,nx.DiGraph())
nx.draw(G)

G.is_directed()
True
环状图
G=nx.cycle_graph(5)
nx.draw(G)

梯状图
G=nx.ladder_graph(5)
nx.draw(G)

线性珠串图
G=nx.path_graph(15)
nx.draw(G)

星状图
G=nx.star_graph(7)
nx.draw(G)

轮辐图
G=nx.wheel_graph(8)
nx.draw(G)

二项树
G=nx.binomial_tree(5)
nx.draw(G)

格栅图
二维矩阵网格图
G=nx.grid_2d_graph(3,5)
nx.draw(G)

多维网格图
G=nx.grid_graph(dim=(2,3,4))
nx.draw(G)

二维三角形网格图
G=nx.triangular_lattice_graph(2,5)
nx.draw(G)

二维六边形蜂窝图
G=nx.hexagonal_lattice_graph(2,3)
nx.draw(G)

N维超立方体图
G=nx.hypercube_graph(4)
nx.draw(G)

NetworkX 内置图
G=nx.diamond_graph()
nx.draw(G)

G=nx.bull_graph()
nx.draw(G)

G=nx.frucht_graph()
nx.draw(G)

G=nx.house_graph()
nx.draw(G)

G=nx.house_x_graph()
nx.draw(G)

G=nx.petersen_graph()
nx.draw(G)

G=nx.krackhardt_kite_graph()
nx.draw(G)

随机图
G=nx.erdos_renyi_graph(10,0.5)
nx.draw(G)

有向图
无标度有向图
G=nx.scale_free_graph(100)
nx.draw(G)

社交网络
G=nx.karate_club_graph()
nx.draw(G,with_labels=True)

G.nodes[5]["club"]
'Mr. Hi'
G.nodes[9]["club"]
'Officer'
G=nx.les_miserables_graph()
plt.figure(figsize=(12,10))
pos=nx.spring_layout(G,seed=10)
nx.draw(G,pos,with_labels=True)

G=nx.florentine_families_graph()
nx.draw(G,with_labels=True)

G=nx.caveman_graph(4,3)
nx.draw(G,with_labels=True)

tree=nx.random_tree(n=10,seed=0)
print(nx.forest_str(tree,sources=[0]))
╙── 0
├── 3
└── 4
├── 6
│ ├── 1
│ ├── 2
│ └── 7
│ └── 8
│ └── 5
└── 9