画有向图
from matplotlib import pyplot as plt
import networkx as nx
# directed graph
G=nx.DiGraph()
G.add_nodes_from([1,2,3])
G.add_edges_from([(1,2),(1,3)])
nx.draw_networkx(G)
plt.show()
调整节点之间的间距
pos = nx.spring_layout(G,k=0.15,iterations=20)
# k controls the distance between the nodes and varies between 0 and 1
# iterations is the number of times simulated annealing is run
# default k =0.1 and iterations=50
from matplotlib import pyplot as plt
import networkx as nx
# directed graph
G=nx.DiGraph()
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')
G.remove_node(3)
G.add_node(1, time='5pm')
print(G.nodes[1]) # 显示{'time': '5pm'}
# print(list(G.nodes))
#
# print(list(G.edges))
# print(G.graph)
# G.nodes[1]['room'] = 714
# G.add_edges_from([(3, 4), (4, 5)], color='red')
# print(G[4][3]['color'])
from matplotlib import pyplot as plt
import networkx as nx
# directed graph
DG = nx.DiGraph()
DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])
print(DG.out_degree(3, weight='weight')) #从该点出去的边的权重
print(DG.in_degree(3, weight='weight')) #进入该点的边的权重
print( DG.degree(1, weight='weight')) # 与该点相连的所有边的权重和
DG[1][2]['color']='red'
nx.draw(DG, with_labels=True, font_weight='bold')
plt.show()