当前位置:网站首页>Networkx usage and nx Draw() related parameters
Networkx usage and nx Draw() related parameters
2022-06-10 20:35:00 【Salted fish lying flat occasionally】
networkx stay 02 year 5 Monthly production , Yes, it is python Software package written in , It is convenient for users to create complex networks 、 Operation and learning . utilize networkx The network can be stored in standardized and non-standard data formats 、 Generate a variety of random networks and classical networks 、 Analyze the network structure 、 Build a network model 、 Design a new network algorithm 、 Network drawing, etc .
Official documents :https://www.osgeo.cn/networkx/reference/introduction.html
Code
First import the package import networkx as nx
Create an empty graph
G = nx.Graph()
Add nodes and edges
G.add_node(1) # Add a single node
G.add_node("x",name='tome') # Add a single node and attributes
G.add_nodes_from([2,3]) # Add multiple nodes from an iteratable container
G.add_nodes_from([(4, {
"color": "red"}), (5, {
"color": "green"})])# Add nodes and attributes
H = nx.path_graph(10) # Create a new map
G.clear() # Empty map
G.add_nodes_from(H) # Add nodes from another diagram
print(list(H.nodes))
print(list(G.nodes))
G.add_edge(1,3)# Add an edge
G.add_edges_from([(2,3),(3,4)])# Add multiple edges
View nodes and edges
list(G.nodes) # Look at the node
for k, v in G.nodes.items(): print(k,v) # View nodes and attributes
G.number_of_nodes() # View several nodes
G.adj[1] # View neighbor nodes
G[1] # View neighbor nodes
list(G.neighbors(1)) # View neighbor nodes
G.number_of_edges() # View several edges
G.add_edge(1,3) # View specific edges
G.edges() # View edges
G.add_edge(2,3)
G.edges([1]) # see 1 All edges connected
G.edges([3]) # see 1 All edges connected
G.degree[1] # Viewing degree
for node,neighbors in g.adjacency():
print(node, neighbors) # View the content of adjacency matrix
Delete nodes and edges
G.remove_node(4)
G.remove_edge(1,3)
Build a new graph from an existing graph
# Build a directed graph
g = nx.DiGraph(G)
nx.draw(g)
Get nodes and edges
G = nx.Graph([(1,2,{
'color':'red'})])
G[1] # obtain 1 Neighbor node
G[1][2] # Get edge properties
G.edges[1,2] # Get edge properties
chart 、 node 、 Edge properties
# Graph level properties
g = nx.Graph(day = 'none')
g.graph # Output graph level attribute information
g.graph['day']= 'tom' # Modify graph level attribute information
g.graph['date']= 'now' # Add graph level attribute information
# Node properties
g.add_node(1, time='now')
g.add_nodes_from([2,3,4,5,6], time='yes')
g.nodes.data()
g.nodes[2]
# Edge properties
g.add_edge(1,2,time='now')
g.add_edges_from([(1,2,{
'time':'now'}),(1,3,{
'time':'naw'})])
G[1][2]['color'] = 'blue'
G.edges[1,2]['color'] ='Y'
g.edges.data()
Analysis chart
# Analysis chart : Connected component
g.clear()
g.add_edges_from([(1,2),(3,2)])
g.add_nodes_from("spam")
list(nx.connected_components(g)) # Yes 5 Connected components
nx.draw(g) # visualization
# Analysis chart : Sort by degrees
sorted(((node,degree) for node,degree in g.degree), key= lambda d:d[1],reverse = True)
Figure Visualization
# Figure Visualization
g = nx.petersen_graph()
nx.draw(g, with_labels = True) # mark label
from edgelist Read graph
# from edgelist Read graph
g = nx.read_edgelist('edglist.txt')
nx.draw(g, with_labels= True)
Save map
plt.savefig("path.png")
networkx–nx.draw() Parameters
x.draw() Method , Accept at least one parameter : The network to be plotted G
Run style
- `node_size`: Specify the size of the node ( The default is 300)
- `node_color`: Specify the color of the node ( The default is red , You can simply identify colors with strings , for example 'r' In red ,'b' For green, etc )
- `node_shape`: The shape of the node ( The default is circle , Use string 'o' identification )
- `alpha`: transparency ( The default is 1.0, Opaque ,0 Is completely transparent )
- `width`: The width of the edge ( The default is 1.0)
- `edge_color`: The color of the edges ( Default is black )
- `style`: The pattern of the sides ( The default is to implement , Optional : solid|dashed|dotted,dashdot)
- `with_labels`: Whether the node is labeled ( The default is True)
- `font_size`: Node label font size ( The default is 12)
- `font_color`: Node label font color ( Default is black )
Use layout :
circular_layout: Nodes are evenly distributed on a ring
random_layout: Nodes are randomly distributed
shell_layout: Nodes are distributed on concentric circles
spring_layout:
use Fruchterman-Reingold The algorithm arranges the nodes ( It looks like multicentric radiation )
spectral_layout: The nodes are arranged according to the Laplacian eigenvector of the graph
To add text :
use plt.title() Method to add a title to the drawing , This method takes a string as an argument .
fontsize Parameter is used to specify the size of the title . for example :plt.title(“BA Networks”, fontsize = 20).
If you want to add text anywhere , You can use plt.text() Method .
边栏推荐
- 【legendre】多项式
- 功耗开发经验分享:设计功耗大板
- canvas 高级功能(上)
- Microsoft Word 教程「5」,如何在 Word 中更改页边距、创建新闻稿栏?
- C语言 浮点数 储存形式
- 国家先进计算产业创新(宜昌)中心正式落地 中科曙光、升哲科技联合运营
- Why do some web page style attributes need a colon ":" and some do not?
- The national advanced computing industry innovation (Yichang) center was officially launched and jointly operated by Zhongke Shuguang and Shengzhe technologies
- Is it safe to open an account online for futures? How to open an account specifically
- vulnhub-The Planets: Earth
猜你喜欢

FS2117升压IC输出5V2.4A同步整流

「Bug」问题分析 RuntimeError:which is output 0 of ReluBackward0

一个10年左右的老程序员说:简单CRUD功能进入无码开发时代1 之 增删改查接口信息
![[technical fragment] implementation of renaming and filtering duplicate name files with suffixes](/img/be/d33cd20ae37dfb5d3ea59ce8b1e568.png)
[technical fragment] implementation of renaming and filtering duplicate name files with suffixes

Redis集群形式--哨兵模式集群和高可用模式集群---Redis学习笔记003

FPGA state machine

测试apk-异常管控netLocation攻击者开发
![[observation] shengteng Zhixing: scene driven, innovation first, press the](/img/53/29f5fd8b869087bafbe74849329c84.jpg)
[observation] shengteng Zhixing: scene driven, innovation first, press the "acceleration key" for Intelligent Transportation
![[enter textbook latex record]](/img/f0/5ca60f0894d4ae544e7399d18a3a42.png)
[enter textbook latex record]

国庆期间给大家推荐一个可能会成为2019最佳的CRUD工具
随机推荐
服务管理与通信,基础原理分析
MySQL backup and manual execution of shell scripts are OK, and crontab scheduled execution fails
Fs4100 lithium battery charging management IC input 12V to 8.4v charging IC
力扣1082,1084题解_sql查询类型的题目
The old programmer said: stop translating the world, developers should return to programming
H5 van-popup全屏弹窗,模拟页面回退效果,支持左上角返回按钮,适用物理返回,侧滑与底部返回键
Rotated Sorted Array旋转排序数组相关题
ESP8266 系统环境搭建
First batch! Sinomenine has passed CWPP capability assessment and inspection of Xintong Institute
Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security
Handwritten code bind
Microsoft Word 教程「5」,如何在 Word 中更改頁邊距、創建新聞稿欄?
Key points of lldp protocol preparation
Recording a super Oolong mental retardation bug may help people like me eat for free
利用阿里云国际购买的服务器搭建个人网站步骤
vulnhub-The Planets: Earth
When can Flink support the SQL client mode? When can I specify the applicati for submitting tasks to yarn
Zabbix_ Monitoring ssh/crond Service - wechat alarm
知识图谱/关系可视化
【观察】昇腾智行:场景驱动,创新先行,为智慧交通按下“加速键”