画线 - 点图

时间:2014-07-30 17:15:12

标签: python numpy matplotlib plot sage

我有:

  • Q NODES = [(x, y)_1, ........, (x, y)_Q]的列表,其中每个元素(x, y)表示2D笛卡尔空间中节点的空间位置。

  • QxQ 矩阵H,其中H[k, l]是连接节点kl的边的长度,以及{如果H[k, l] == 0k未连接,则{1}}。

  • QxQ 矩阵l,其中Z是连接节点Z[k, l]和{{1}的边的标量“强度”值}。如果kl未连接,则再次Z[k, l] == 0

我想很好地绘制空间位置的节点,通过边缘连接,并使用色标来表示“强度”。

我该怎么做? (我使用python,sage,matplotlib和numpy)

1 个答案:

答案 0 :(得分:1)

这是一个示例函数,它只使用numpy和matplotlib来绘制带有由colormap表示的边权重的无向图形:

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)数组,尽管您可以使用{{1}轻松获取此值}。我暂时也忽略了H,因为节点之间的欧氏距离由(x, y)位置给出了隐含性。您可以始终以H以其他方式表示值,例如使用linewidths的{​​{1}}。

这是一个快速演示:

LineCollection

输出:

enter image description here

请注意,此示例仅适用于无向图表。如果同时存在# 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]l之间会有两条重叠的线,因此如果两条边的强度值不同,则无法实现用它们的颜色来区分。

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

相关问题