这是一个示例函数,它仅使用 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 中的值,例如使用LineCollection 的linewidths。
这是一个快速演示:
# 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 和k 之间将绘制两条重叠的线,因此如果两条边的强度值不同,它们将无法通过颜色区分。
有很多专门的 Python 库可用于构建、分析和绘制图形,例如 igraph、graphtool 和 networkx,它们能够很好地绘制有向图。