我有:


Q NODES = [(x, y)_1, ........, (x, y)_Q]的列表,其中每个元素(x, y)表示2D笛卡尔空间中节点的空间位置。
QxQ矩阵H,其中H[k, l]是连接节点kl的边缘的长度,如果H[k, l] == 0k未连接,则l
一个QxQ矩阵Z,其中Z[k, l]是连接节点kl的边的标量“强度”值。同样,如果Z[k, l] == 0k未连接,则为l


我想很好地在节点的空间位置上绘制节点,这些节点由边缘连接,并使用色标表示“强度”。

我怎样才能做到这一点? (我使用python,sage,matplotlib和numpy)

最佳答案

这是一个示例函数,仅使用numpy和matplotlib绘制无向图,其边缘权重由色图表示:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

def plot_undirected_graph(xy, z):

    fig, ax = plt.subplots(1, 1)
    ax.hold(True)

    # the indices of the start, stop nodes for each edge
    i, j = np.where(z)

    # an array of xy values for each line to draw, with dimensions
    # [nedges, start/stop (2), xy (2)]
    segments = np.hstack((xy[i, None, :], xy[j, None, :]))

    # the 'intensity' values for each existing edge
    z_connected = z[i, j]

    # this object will normalize the 'intensity' values into the range [0, 1]
    norm = plt.Normalize(z_connected.min(), z_connected.max())

    # LineCollection wants a sequence of RGBA tuples, one for each line
    colors = plt.cm.jet(norm(z_connected))

    # we can now create a LineCollection from the xy and color values for each
    # line
    lc = LineCollection(segments, colors=colors, linewidths=2,
                        antialiased=True)

    # add the LineCollection to the axes
    ax.add_collection(lc)

    # we'll also plot some markers and numbers for the nodes
    ax.plot(xy[:, 0], xy[:, 1], 'ok', ms=10)
    for ni in xrange(z.shape[0]):
        ax.annotate(str(ni), xy=xy[ni, :], xytext=(5, 5),
                    textcoords='offset points', fontsize='large')

    # to make a color bar, we first create a ScalarMappable, which will map the
    # intensity values to the colormap scale
    sm = plt.cm.ScalarMappable(norm, plt.cm.jet)
    sm.set_array(z_connected)
    cb = plt.colorbar(sm)

    ax.set_xlabel('X position')
    ax.set_ylabel('Y position')
    cb.set_label('Edge intensity')

    return fig, ax


为简单起见,我已将您的NODES变量的格式更改为仅是(n_nodes, 2)值的(x, y)数组,尽管您可以使用np.array(NODES)轻松获得。我暂时也忽略了H,因为节点之间的欧几里得距离由它们的(x, y)位置隐式给出。您总是可以用其他方式表示H中的值,例如使用linewidthsLineCollection

这是一个快速演示:

# some random xy positions:
xy = np.random.rand(10, 2)

# a random adjacency matrix
adj = np.random.poisson(0.2, (10, 10))

# we multiply by this by a matrix of random edge 'intensities'
z = adj * np.random.randn(*adj.shape)

# do the plotting
plot_undirected_graph(xy, z)


输出:



请注意,此示例仅真正适用于无向图。如果同时存在Z[k, l]Z[l, k],则在节点lk之间将绘制两条重叠的线,因此,如果两个边缘的强度值不同,则将无法通过颜色区分它们。

有许多专用的Python库可用于构造,分析和绘制图,例如igraphgraphtoolnetworkx,它们能够很好地绘制有向图。

关于python - 画线-点图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25043002/

10-12 20:13