0

Matrice

Hey all, I've got matrice of adjacency and I have to draw a graph in Python. My code is looking like this, but it still doesn't work...

import matplotlib.pyplot as plt
## G = nx.Graph()
G = nx.MultiGraph()
## G = nx. iGraph()
nodes=["0", "1", "2", "3", "4"]
G.add_nodes_from(nodes)
G.add_edge("0","3")
G.add_edge("0","3")
G.add_edge("0","3")
G.add_edge("0","1")
G.add_edge("1","2")
G.add_edge("1","4")
G.add_edge("1","4")
G.add_edge("3","4")
print(G.nodes())
print(G.edges())
nx.draw(G, with_labels = True)
plt.savefig("simple_path.png")
plt.show()```
  • 1
    Could you be more specific than "it still doesn't work..." ? What, exactly, is the problem? Are you getting an error message? If so what message and on what line? Is it running but not giving the desired output? How so? What is the desired output and what it the current output? – John Coleman May 06 '20 at 23:34
  • There should be 8 edges, but on picture done by Python I got only 5. Moreover, I don't know how to add multiple edges between vertices. Looking at matrice we can conclude that there 3 edges between vertices 0 and 3, but even though I put three times "G.add_edge("0","3")" it still shows only one edge. That's the problem. – Limster612 May 06 '20 at 23:39
  • This is current output: https://m.imgur.com/a/q6HCemN – Limster612 May 06 '20 at 23:47
  • An adjacency matrix should be square. It will only show a single edge between two vertices. – Code-Apprentice May 06 '20 at 23:48
  • Sorry, I am not native speaker, but I meant other matrix. Rows are vertices and columns are edges. Is it still possible to draw it correctly? – Limster612 May 06 '20 at 23:51
  • Does this answer your question? [how to draw multigraph in networkx using matplotlib or graphviz](https://stackoverflow.com/questions/14943439/how-to-draw-multigraph-in-networkx-using-matplotlib-or-graphviz) – Code-Apprentice May 06 '20 at 23:52
  • See if the above link helps answer your question. – Code-Apprentice May 06 '20 at 23:52
  • I couldn't remember the name of this matrix, either. Finally found it: [Incidence Matrix](https://en.wikipedia.org/wiki/Incidence_matrix). – Code-Apprentice May 06 '20 at 23:54
  • Okay, but post is 7 years old. Syntax is a bit different and I don't know how to apply it to my graph :/ – Limster612 May 06 '20 at 23:58

0 Answers0