user12217822
user12217822

Reputation: 302

How to show graph with matrix in python?

I want to show graph with matrix below. I've saved this matrix in excel file to import that to gephi application but it doesn't work . How can I show it?

0 0 0 1 0 0
0 0 1 0 0 0
0 1 0 1 0 0 
1 0 1 0 1 2
0 0 0 1 0 1
0 0 0 2 1 0

Upvotes: 0

Views: 1150

Answers (2)

Glycerine
Glycerine

Reputation: 7347

I took some extra days to research this one and I found matrix-47, of which allows printing and managing these matrix formats:

https://github.com/AnonymouX47/matrix

>>> from matrix import Matrix
>>> print(Matrix(4, 4))
+―――――――――――――――+
| 0 | 0 | 0 | 0 |
|―――+―――+―――+―――|
| 0 | 0 | 0 | 0 |
|―――+―――+―――+―――|
| 0 | 0 | 0 | 0 |
|―――+―――+―――+―――|
| 0 | 0 | 0 | 0 |
+―――――――――――――――+

>>> from matrix import *
>>> m = randint_matrix(3, 3, range(-9, 10))
>>> print(m)
+――――――――――――――+
| 9  | -1 | 9  |
|――――+――――+――――|
| 2  | -8 | -1 |
|――――+――――+――――|
| -4 | 0  | 9  |
+――――――――――――――+

Upvotes: 0

Frodnar
Frodnar

Reputation: 2252

import numpy as np
import networkx as nx


matrix = np.array([ [0,0,0,1,0,0]
                   ,[0,0,1,0,0,0]
                   ,[0,1,0,1,0,0]
                   ,[1,0,1,0,1,2]
                   ,[0,0,0,1,0,1]
                   ,[0,0,0,2,1,0]])

G = nx.from_numpy_array(matrix)

nx.draw(G, with_labels=True)

enter image description here

Upvotes: 2

Related Questions