# #!-*- coding:utf8-*-

# import scipy as sp
# import scipy.sparse # call as sp.sparse
import networkx as nx
import matplotlib.pyplot as plt

print(nx.__version__)

G = nx.Graph()
G.add_edge(5, 1, weight=7)
G.add_edge(5, 3, weight=8)
G.add_edge(1, 2, weight=6)
G.add_edge(1, 3, weight=3)
G.add_edge(3, 2, weight=4)
G.add_edge(3, 4, weight=3)
G.add_edge(2, 4, weight=2)
G.add_edge(2, 6, weight=5)
G.add_edge(4, 6, weight=2)

print(G.edges(data=True))
data = {(u, v): weight['weight'] for (u, v, weight) in G.edges(data=True)}
print(data)
# pos = nx.spectral_layout(G)
pos = {1: [0, 0],
2: [5, 0],
3: [0, -5],
4: [5, -5],
5: [-2.5, -2.5],
6: [7.5, -2.5],
}


nx.draw_networkx_nodes(G, pos, node_size=900)
nx.draw_networkx_edges(G, pos, width=3)
nx.draw_networkx_labels(G, pos, font_size=20)
nx.draw_networkx_edge_labels(G, pos, data, font_size=20)
plt.show()
print((nx.to_scipy_sparse_array(G)).toarray())
# output
2.7.1
[(5, 1, {'weight': 7}), (5, 3, {'weight': 8}), (1, 2, {'weight': 6}), (1, 3, {'weight': 3}), (3, 2, {'weight': 4}), (3, 4, {'weight': 3}), (2, 4, {'weight': 2}), (2, 6, {'weight': 5}), (4, 6, {'weight': 2})]
{(5, 1): 7, (5, 3): 8, (1, 2): 6, (1, 3): 3, (3, 2): 4, (3, 4): 3, (2, 4): 2, (2, 6): 5, (4, 6): 2}
[[0 7 8 0 0 0]
[7 0 3 6 0 0]
[8 3 0 4 3 0]
[0 6 4 0 2 5]
[0 0 3 2 0 2]
[0 0 0 5 2 0]]

networkx绘制简单图_python