一、简单使用示例

输入参数主要包括:
PointsName: 列表结构,表示图各个顶点的序号(用于标记注释)
AdjacencyMatrix: 二维列表结构,表示图的邻接矩阵
注:(设计时主要考虑的有向图,如果表示无向图,权值的标注需要改一下)

if __name__ == '__main__':
    # 示例
    a = [1, 2, 3, 4]  # 点的序号,用于标注
    b = [[0, 3, 0, 6], [1, 0, 1, 2], [0, 4, 0, 0], [2, 1, 0, 0]]  # 邻接矩阵
    AdjacencyMatrixToGraph(a, b)

运行结果:在这里插入图片描述

二、主要函数

import matplotlib.pyplot as plt
import numpy as np


def AdjacencyMatrixToGraph(PointsName, AdjacencyMatrix):
    """
    根据邻接矩阵绘制图结构
    :param PointsName: 列表结构,表示图各个顶点的序号(用于标记注释)
    :param AdjacencyMatrix: 二维列表结构,表示图的邻接矩阵
    """

    num = len(PointsName)

    fig = plt.figure()
    ax = fig.add_subplot(111)
    xlim = [-1.1, 1.1]
    ylim = [-1.1, 1.1]
    ax.set(xlim=xlim, ylim=ylim)  # 初始化坐标系

    theta = 2 * np.pi / num
    x = []
    y = []
    for i in range(num):
        x.append(np.cos(i * theta))
        y.append(np.sin(i * theta))  # 分配点位

    ax.scatter(x, y, color='b', marker='.')

    for i in range(num):
        ax.annotate(PointsName[i], (x[i], y[i]))  # 绘制点并加标注

    for i in range(num):
        for j in range(num):
            if AdjacencyMatrix[i][j]:  # 如果有连线
                x_temp = [x[i], x[j]]
                y_temp = [y[i], y[j]]
                x_text = (x_temp[0] * 2 + x_temp[1]) / 3
                y_text = (y_temp[0] * 2 + y_temp[1]) / 3
                if i == j:
                    x_text -= 0.05
                    y_text -= 0.05
                ax.plot(x_temp, y_temp, linewidth=0.5, color='red')
                ax.text(x_text, y_text, "W:"+str(AdjacencyMatrix[i][j]))  # 连线加权注释

    ax.axis('off')  # 不显示坐标轴
    plt.show()

三、原始代码

import matplotlib.pyplot as plt
import numpy as np


def AdjacencyMatrixToGraph(PointsName, AdjacencyMatrix):
    """
    根据邻接矩阵绘制图结构
    :param PointsName: 列表结构,表示图各个顶点的序号(用于标记注释)
    :param AdjacencyMatrix: 二维列表结构,表示图的邻接矩阵
    """

    num = len(PointsName)

    fig = plt.figure()
    ax = fig.add_subplot(111)
    xlim = [-1.1, 1.1]
    ylim = [-1.1, 1.1]
    ax.set(xlim=xlim, ylim=ylim)  # 初始化坐标系

    theta = 2 * np.pi / num
    x = []
    y = []
    for i in range(num):
        x.append(np.cos(i * theta))
        y.append(np.sin(i * theta))  # 分配点位

    ax.scatter(x, y, color='b', marker='.')

    for i in range(num):
        ax.annotate(PointsName[i], (x[i], y[i]))  # 绘制点并加标注

    for i in range(num):
        for j in range(num):
            if AdjacencyMatrix[i][j]:  # 如果有连线
                x_temp = [x[i], x[j]]
                y_temp = [y[i], y[j]]
                x_text = (x_temp[0] * 2 + x_temp[1]) / 3
                y_text = (y_temp[0] * 2 + y_temp[1]) / 3
                if i == j:
                    x_text -= 0.05
                    y_text -= 0.05
                ax.plot(x_temp, y_temp, linewidth=0.5, color='red')
                ax.text(x_text, y_text, "W:"+str(AdjacencyMatrix[i][j]))  # 连线加权注释

    ax.axis('off')  # 不显示坐标轴
    plt.show()


if __name__ == '__main__':
    # 示例
    a = [1, 2, 3, 4]  # 点的序号,用于标注
    b = [[0, 3, 0, 6], [1, 0, 1, 2], [0, 4, 0, 0], [2, 1, 0, 0]]  # 邻接矩阵
    AdjacencyMatrixToGraph(a, b)
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐