栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

图可视化networkx

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

图可视化networkx

  1. 导入包networkxpip install networkx
  2.  #创建一个没有边和结点的空图
     import networkx as nx
     G = nx.Graph()
     #添加一个结点
     G.add_node(1)
     #添加多个结点,可以从list,也可以从tuple中添加
     G.add_nodes_from([2,3])
     #添加结点属性,通过元组实现
     G.add_nodes_from([
     (4, {"color": "red"}),
     (5, {"color": "green"})])
     #添加边
     G.add_edge(1,2)
     G.add_edge(*(2,3))
     #添加多条边,可以用元组包含list,也可以用list包含元组
     G.add_edges_from(([3,4],[1,5],[2,4]))
     G.add_edges_from([(1,3), (2,5)])
     #添加一个用"node"命名的结点
     G.add_node("node")
     #添加结点"node"与其他数字结点的联系,这个结点的名字不会表现出来
     G.add_edges_from([("node",1), ("node", 3), ("node", 5)])
     #查看图的结点个数和边个数
     print(G.number_of_nodes(), G.number_of_edges())
     
     #图的四个属性
     print(list(G.nodes))
     #[1, 2, 3, 4, 5, 'node']
     print(list(G.edges))
     #[(1, 2), (1, 5), (1, 3), (1, 'node'), (2, 3), (2, 4), (2, 5), (3, 4), (3, 'node'), (5, 'node')]
     print(list(G.adj["node"]))
     #[1, 3, 5],结点"node"的邻接结点
     print(G.degree["node"])
     #3 ,结点"node"的度
    
```python
pandas 与 图
cora_graph = nx.from_pandas_edgelist(citations.sample(n=1500))#可以直接将pandas中数据边导入图中
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/665898.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号