我是靠谱客的博主 仁爱舞蹈,最近开发中收集的这篇文章主要介绍networkx绘制简单图,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

# #!-*- 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绘制简单图的全部内容,希望文章能够帮你解决networkx绘制简单图所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部