此种类型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()
如图:
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/zhangphil/article/details/121150370
内容来源于网络,如有侵权,请联系作者删除!