。This code works:

n = adyacency_mathix.shape[0]
axis = np.linspace(0, 2*np.pi, n, endpoint=False)
x, y = np.cos(axis), np.sin(axis)
for i in xrange(n):
    for j in xrange(i + 1, n):
        if self.matrix[i, j] == 1:
            pyplot.plot((x[i], x[j]), (y[i], y[j]), color = 'blue')
pyplot.show()

但可以优化。

最佳答案

如果您只想减少编写的代码量,那么您可能会对流行的networkx项目感兴趣。

import matplotlib.pyplot as plt
import networkx as nx

# Generating sample data
G = nx.florentine_families_graph()
adjacency_matrix = nx.adjacency_matrix(G)

# The actual work
# You may prefer `nx.from_numpy_matrix`.
G2 = nx.from_scipy_sparse_matrix(adjacency_matrix)
nx.draw_circular(G2)
plt.axis('equal')

python - 给定一个邻接矩阵,如何使用matplotlib绘制图形?-LMLPHP

关于python - 给定一个邻接矩阵,如何使用matplotlib绘制图形?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44271504/

10-12 16:55