0

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
user12217822
  • 292
  • 1
  • 5
  • 16
  • Would you like to show an Adjacency matrix? – Glycerine Oct 05 '21 at 14:31
  • @Glycerine Yes , I want to show a Adjacency matrix – user12217822 Oct 05 '21 at 14:33
  • @Glycerine I may ask you to help me – user12217822 Oct 05 '21 at 15:01
  • Does a `2` in your matrix represent double edge weight or two edges? Do you care if this is shown in the graph? – Frodnar Oct 05 '21 at 16:15
  • @Frodnar It represent two edges – user12217822 Oct 05 '21 at 16:18
  • 1
    Ah, networkx doesn't have good native support for visualizing multiple parallel edges as evidenced by multiple Stack Overflow threads ([1](https://stackoverflow.com/questions/56543559/networkx-drawing-parallel-edges) and [2](https://stackoverflow.com/questions/22785849/drawing-multiple-edges-between-two-nodes-with-networkx), for example). Those linked threads provide several options for workarounds / other packages to consider. – Frodnar Oct 06 '21 at 09:28

2 Answers2

2
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

Frodnar
  • 2,129
  • 2
  • 6
  • 20
  • I got this error: "NetworkXError: random_state_index is incorrect" – user12217822 Oct 05 '21 at 16:15
  • Ah yeah, this is a known bug. I believe upgrading `decorator` module will fix it. See [here](https://stackoverflow.com/questions/66920533/networkx-shows-random-state-index-is-incorrect/66921155#66921155). – Frodnar Oct 05 '21 at 16:21
  • I have seen this link before but I do not know where to change the version? – user12217822 Oct 05 '21 at 16:27
  • How do you install python packages like `networkx`? `conda` and `pip` are the most common methods from the command line. So probably one of: `pip install decorator --upgrade` or `conda update decorator` – Frodnar Oct 05 '21 at 17:04
  • I tried different versions for this package but unfortunately it still does not work – user12217822 Oct 05 '21 at 17:20
  • 1
    Hmmm. It still sounds like a package conflict / installation issue. I would try installing in a fresh install in a clean virtual environment if possible. – Frodnar Oct 06 '21 at 09:30
0

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  |
+――――――――――――――+
Glycerine
  • 7,157
  • 4
  • 39
  • 65