3

I have an undirected graph described by its adjacency matrix (a numpy array) and I want to plot it, with vertices placed in a n-regular polygon. 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()

but can be optimized.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
Diego Silvera
  • 201
  • 1
  • 2
  • 13
  • What is the question? Please edit you post to actually provide a clear problem description and a question. See [ask] and [mcve]. Otherwise the question needs to be closed. – ImportanceOfBeingErnest May 30 '17 at 20:46
  • @ImportanceOfBeingErnest I want and optimized version of this code – Diego Silvera May 30 '17 at 20:51
  • @DiegoSilvera Define "optimized". You can optimize for a large number of metrics. If you're concerned about time, the drawing will take far longer than the computation anyway, but profile it for yourself to identify the bottlenecks. – Arya McCarthy May 30 '17 at 21:03

1 Answers1

11

You may be interested in the popular networkx project, if you're interested in simply reducing the amount of code you write.

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')

florentine circular graph

Disclaimer: I am a contributor to networkx.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
  • Thank you!! Is there a way to label the vertices? – Diego Silvera May 31 '17 at 11:23
  • 2
    There sure is. The documentation for [`draw_circular`](https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.drawing.nx_pylab.draw_circular.html#networkx.drawing.nx_pylab.draw_circular) points out that you can use the keyword args from [`draw_networkx`](https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.drawing.nx_pylab.draw_networkx.html#networkx.drawing.nx_pylab.draw_networkx), one of which would be `with_labels=True`. – Arya McCarthy May 31 '17 at 14:27
  • Hi, could you be so kind and update your answer since it seems that networkx has changed I currently receive executing "nx.adjacency_matrix(G)" No module named 'scipy' so I guess the whole function is deprecated? – baxbear Jan 17 '20 at 15:36
  • Hi, that doesn't mean it's deprecated. You just need to install `scipy` also, so that `networkx` can import and use it. I've found the easiest way to do this is with an environment manager like `conda`. You can in one line do `conda create --name myenvironment python=3.7 scipy networkx`. – Arya McCarthy Jan 26 '20 at 19:03
  • In networkx 3.0, the function is `nx.from_scipy_sparse_array(adjacency_matrix)` – Tillson Mar 08 '23 at 05:54