networkx
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edge(1, 2, weight=5)
G.add_edge(1, 3, weight=6)
G.add_edge(1, 4, weight=2)
G.add_edge(2, 4, weight=1)
G.add_edge(1, 4, weight=0.5)
# positions for all nodes using, Fruchterman-Reingold force-directed algorithm
# spring_layout 이나 kamada_kawai_layout이 젤 낫다.
pos=nx.spring_layout(G)
nx.draw(G, pos=pos, with_labels=True)
# Add edge labels
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
'''
Node Color
https://goo.gl/4ETMpV
size 300 default
node_shape, alpha, cmap, vmin, vmax, ax, linewidths, label, **kwds
'''
nx.draw_networkx_nodes(G, pos,
nodelist=[1, 2], # 노드 이름
node_color='#FF1744', # 기본 'r', 'g', 'b' 색 지원
node_size=1000)
nx.draw_networkx_nodes(G, pos,
nodelist=[3, 4],
node_color='#673AB7',
node_size=1000)
# Save graph as png file
plt.savefig("graph.png")
# Show
plt.show()
import matplotlib.pyplot as plt
import networkx as nx
G = nx.cycle_graph(24)
pos = nx.spring_layout(G, iterations=200)
nx.draw(G, pos, node_color=range(24), node_size=800, cmap=plt.cm.Blues)
plt.show()