this package works together with matplotlib.
1import networkx as nx2from matplotlib import pyplot as plt3
4g = nx.Graph()5
6g.add_node(1)7
8list(g.nodes())9
10g.add_nodes_from([3, 4])11
12print(g.nodes)13
14nx.draw(g)15nx.draw(g, with_labels=True)51 collapsed lines
16plt.show()17
18g.add_node('coin')19
20g.add_edge(1, 2)21e = (2, 3)22g.add_edge(*e)23
24g.add_edges_from([(1, 3), (1, 4), (1, 5)])25
26G.remove_node(2)27G.remove_nodes_from([4, 5, H, 'coin'])28print("les sommets de G sont : ", G.nodes)29
30edgelist = [(0, 1), (1, 2), (2, 3)]31I = nx.Graph(edgelist)32
33edgelist1 = [(0, 1), (0, 2), (0, 3)]34edgelist2 = [(0, 4), (0, 5), (0, 6)]35G1 = nx.Graph(edgelist1)36G2 = nx.Graph(edgelist2)37H1 = nx.disjoint_union(G1, G2) # make two graph in the same one38H2 = nx.compose(G1, G2) # make the same considering the node id39
40g = nx.petersen_graph() # a special graph41
42nx.draw_shell(g, nlist=[range(5, 10), range(5)]) # make 0, 1, 2, 3, 4 in a shell43
44options = {45 'node_color': 'yellow',46 'node_size': 500,47 'width': 2,48 'with_labels': True49}50
51nx.draw_random(G, **options)52plt.show()53
54nx.draw_circular(G, **options)55plt.show()56
57nx.draw_shell(G, nlist=[range(5, 10), range(5)], **options)58plt.show()59
60nx.draw(G)61plt.savefig("graph.png")62
63G2= nx.DiGraph ()64G2.add_edges_from([(0, 1),(1,0), (0, 2), (0, 3)])65nx.draw_random(G2, with_labels=True, font_weight='bold')66plt.show()
draw in particular position
1G=nx.Graph()2G.add_edges_from([[1,2],[2,3],[3,1]])3position={1:[0,2],2:[2,0],3:[0,0]} #dictionnaire où on précise les coordonnées de chacun des sommets4nx.draw(G,pos=position)