networkx节点2D网格,Python

此种类型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()

如图:

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值