In [12]:
import networkx as nx
G=nx.Graph()
G.add_edge(1,"Anna")

G.add_edge(1,"Petya")

G.add_edge(1,"John")


# G.add_edge(2,3)
print(G.edges())
print(G.nodes())

import matplotlib.pyplot as plt

# nx.draw(G, with_labels=True,width=10, node_size=2000, node_color="orange", edge_color="lightblue",alpha=1.0)

nx.draw(G, 
        with_labels=True,
        width=10, 
        node_size=2000, 
        node_color="orange", 
        alpha=1.0)

plt.show()


[(1, 'Anna'), (1, 'Petya'), (1, 'John')]
[1, 'Anna', 'Petya', 'John']

In [38]:
import networkx as nx
from networkx.drawing.nx_agraph import write_dot, graphviz_layout
import matplotlib.pyplot as plt

G=nx.DiGraph()
G.add_edge(1,"Petya")
G.add_edge(1,"John")
G.add_edge(1,"Anna")

G.add_edge("John",4)
G.add_edge("John",5)





# G.add_edge(2,3)
print(G.edges())
print(G.nodes())


c


[(1, 'Petya'), (1, 'John'), (1, 'Anna'), ('John', 4), ('John', 5)]
[1, 'Petya', 'John', 'Anna', 4, 5]

In [9]:
# Author: Aric Hagberg (hagberg@lanl.gov)
import matplotlib.pyplot as plt
import networkx as nx

G = nx.house_graph()
# explicitly set positions
pos = {0: (0, 0),
       1: (1, 0),
       2: (0, 1),
       3: (1, 1),
       4: (0.5, 2.0)}

nx.draw_networkx_nodes(G, 
                       pos, 
                       node_size=2000, nodelist=[4])
nx.draw_networkx_nodes(G, pos, node_size=1000, nodelist=[0, 1, 2, 3], node_color='b')
nx.draw_networkx_edges(G, pos, alpha=0.5, width=6)
plt.axis('on')
plt.show()



In [22]:
import networkx as nx
from networkx.drawing.nx_agraph import write_dot, graphviz_layout
import matplotlib.pyplot as plt
G = nx.DiGraph()

G.add_node("ROOT")

for i in range(5):
    G.add_node("Child_%i" % i)
    G.add_node("Grandchild_%i" % i)
    G.add_node("Greatgrandchild_%i" % i)

    G.add_edge("ROOT", "Child_%i" % i)
    G.add_edge("Child_%i" % i, "Grandchild_%i" % i)
    G.add_edge("Grandchild_%i" % i, "Greatgrandchild_%i" % i)

# write dot file to use with graphviz
# run "dot -Tpng test.dot >test.png"
# write_dot(G,'test.dot')

# same layout using matplotlib with no labels
# plt.title('draw_networkx')
pos =graphviz_layout(G, prog='dot')
nx.draw(G, pos, with_labels=False, arrows=True)
#plt.savefig('nx_test.png')