此种类型2D网格图,类似于棋盘等。
import networkx as nx
import matplotlib.pyplot as plt
def my_graph():
G = nx.grid_2d_graph(4, 4)
pos = nx.spring_layout(G, iterations=100)
# nrows=2,ncols=2,index=1
plt.subplot(2, 2, 1)
nx.draw(G, pos, font_size=10, with_labels=True)
# nrows=2,ncols=2,index=2
plt.subplot(2, 2, 2)
nx.draw(G, pos, node_color="yellow", node_size=50, with_labels=False)
# nrows=2,ncols=2,index=3
plt.subplot(2, 2, 3)
H = G.to_directed()
nx.draw(H, pos, node_color="blue", node_size=20, with_labels=False)
# nrows=2,ncols=2,index=4
plt.subplot(2, 2, 4)
pos = dict((n, n) for n in G.nodes())
labels = dict(((i, j), 'Phil') for i, j in G.nodes())
nx.draw_networkx(G, pos=pos, labels=labels, font_size=8, font_color='white', node_color="red", node_size=350,
width=3)
plt.axis('off')
plt.show()
如图: