Mikul
Mikul

Reputation: 161

Creating graph class

How can i draw new class called flow graph. I got a lot of errors:

Traceback (most recent call last):
  File "Graphs.py", line 26, in <module>
    nx.draw(F1)
  File "/home/thinkpad/anaconda3/lib/python3.4/site-packages/networkx/drawing/nx_pylab.py", line 131, in draw
    draw_networkx(G, pos=pos, ax=ax, **kwds)
  File "/home/thinkpad/anaconda3/lib/python3.4/site-packages/networkx/drawing/nx_pylab.py", line 262, in draw_networkx
    pos = nx.drawing.spring_layout(G)  # default to spring layout
  File "/home/thinkpad/anaconda3/lib/python3.4/site-packages/networkx/drawing/layout.py", line 232, in fruchterman_reingold_layout
    if len(G)==0:
TypeError: object of type 'FlowGraph' has no len()

The code:

import networkx as nx
import matplotlib.pyplot as plt

class FlowGraph:
    G=nx.DiGraph()
    I=[]
    O=[]

F1=FlowGraph()
# Add nodes
F1.G.add_node(1)
F1.G.add_node(2)
F1.G.add_node(3) 
# Add edges
F1.G.add_edge(1,2)
#F1.G.add_edges_from[(1,2),(2,3)]
# Add interface
F1.I=[1]
F1.O=[3]
nx.draw(F1)
plt.show()

Upvotes: 0

Views: 168

Answers (1)

Bach
Bach

Reputation: 6217

The traceback tells you that the function nx.draw has failed. If you look at the docstring of nx.draw you will see that it expects a networkx graph as a first argument. Instead, you've provided it a FlowGraph instance. The function nx.draw simply doesn't know what to do with your FlowGraph.

Perhaps you wish to pass F1.G into nx.draw? Note that F1.G is a networkx graph.

Upvotes: 1

Related Questions