In [1]:
# Import networkx and also matplotlib.pyplot for visualization
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
In [12]:
# Create an empty undirected graph
G = nx.Graph()
In [13]:
# Add some nodes and edges. Adding edges aslo adds nodes if they don't already exist.
G.add_node('Janos')
G.add_nodes_from(['Sophie', 'Otto'])
G.add_edge('Janos', 'Sophie')
G.add_edges_from([('Janos', 'Otto'), ('Sophie', 'Otto')])
In [14]:
nx.draw_spectral(G, with_labels=True, node_size=3000)
plt.show()
In [15]:
# Create an empty directed graph
G = nx.DiGraph()
In [16]:
# Add some nodes and edges. Adding edges aslo adds nodes if they don't already exist.
G.add_node('Janos')
G.add_nodes_from(['Sophie', 'Otto'])
G.add_edge('Janos', 'Sophie')
G.add_edges_from([('Janos', 'Otto'), ('Sophie', 'Otto')])
In [17]:
nx.draw_spectral(G, with_labels=True, node_size=3000)
plt.show()
In [18]:
import numpy as np
G.add_node(np.mean)
file = open('abc.txt', 'w')
G.add_node(file)
print(G.nodes())
In [23]:
!head addHealth81.txt
In [24]:
D = nx.read_weighted_edgelist('addHealth81.txt', create_using=nx.DiGraph())
In [25]:
len(D.nodes()), len(D.edges())
Out[25]:
In [ ]: