无向图

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(
    [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
     ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])

val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0}

values = [val_map.get(node, 0.25) for node in G.nodes()]

nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values,with_labels=True)
plt.show()

在这里插入图片描述

import networkx as nx 导包

G = nx.Graph() Create an empty graph with no nodes and no edges

Node

add_node() add one node

G.clear()
import networkx as nx
G = nx.Graph()
G.add_node(1)
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

G.clear()
import networkx as nx
G = nx.Graph()
H = nx.path_graph(10)
G.add_node(H)
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

print(type(H))
<class 'networkx.classes.graph.Graph'>
G.clear()
import networkx as nx
G = nx.Graph()
G.add_node("spam")        # adds node "spam"
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

add_nodes_from() add a list of nodes

G.clear()
import networkx as nx
G = nx.Graph()
G.add_nodes_from([2, 3, 4, 5])
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

G.clear()
import networkx as nx
G = nx.Graph()
G.add_nodes_from("spam")  # adds 4 nodes: 's', 'p', 'a', 'm'
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

G.clear()
import networkx as nx
G = nx.Graph()
H = nx.path_graph(10)
G.add_nodes_from(H)
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

Edges

add_edge() adding one edge

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edge(3, 'm')
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2)
e = (2, 3)
G.add_edge(*e)  # unpack edge tuple*
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

adding a list of edges

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3)])
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

G.clear()
import networkx as nx
G = nx.Graph()
H = nx.path_graph(10)
G.add_edges_from(H.edges)
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

Graph attributes

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3)])
G.add_node(1)
G.add_edge(1, 2)
G.add_node("spam")        # adds node "spam"
G.add_nodes_from("spam")  # adds 4 nodes: 's', 'p', 'a', 'm'
G.add_edge(3, 'm')
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

图的节点属性

G.number_of_nodes()
8
list(G.nodes)
[1, 2, 3, 'spam', 's', 'p', 'a', 'm']

图的边属性

G.number_of_edges()
3
list(G.edges)
[(1, 2), (1, 3), (3, 'm')]
G.edges([2, 'm',1])
EdgeDataView([(2, 1), ('m', 3), (1, 3)])
G.edges[1, 2]
{}
G[1][2]
{}

节点的邻居

list(G.adj[1])
[2, 3]
list(G.neighbors(1))
[2, 3]
list(G.adj[3])
[1, 'm']
list(G.neighbors(3))
[1, 'm']
list(G.adj['spam'])
[]
list(G.neighbors('spam'))
[]
G[1]  # same as G.adj[1]
AtlasView({2: {}, 3: {}})

节点的度(degrees of nodes)

G.degree[1]  # the number of edges incident to 1
2
G.degree['spam']
0
G.degree([2, 3,'spam'])
DegreeView({2: 1, 3: 2, 'spam': 0})

移除节点

移除前的原始图
在这里插入图片描述

G.remove_node(2)
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

G.remove_node('spam')
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

G.remove_nodes_from("spam")
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

移除边

G.remove_edge(1, 3)
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

颜色属性

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2, weight=4.7 )
G.add_edges_from([(3, 4), (4, 5)], color='red')
G.add_edges_from([(1, 2, {'color': 'blue'}), (2, 3, {'weight': 8})])
G[1][2]['weight'] = 4.7
G.edges[3, 4]['weight'] = 4.2
nx.draw(G,with_labels=True,node_color='green')
plt.show()

在这里插入图片描述

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(
    [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
     ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])

val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0}

values = [val_map.get(node, 0.25) for node in G.nodes()]

nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values,with_labels=True)
plt.show()

在这里插入图片描述
对代码进行解读

dict = {'Name': 'Zara', 'Age': 27}
print(dict.get('Name'))
print(dict.get('mikowoo',0.25))
Zara
0.25
values = [val_map.get(node, 0.25) for node in G.nodes()]
print(values)
[0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25]
val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0}

values = [val_map.get(node, 0.25) for node in G.nodes()]
print(values)
print(G.nodes())
[1.0, 0.25, 0.25, 0.5714285714285714, 0.25, 0.25, 0, 0.25]
['A', 'B', 'C', 'D', 'E', 'F', 'H', 'G']
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值