标签:
最近在看一些算法,需要将抽象的图的数据结构用可视化的方式展现,本来以为matplotlib可能会有潜在的支持,结果发现了一个关于图的计算和展示的模块networkx。这个模块使用非常方便,支持基于dict的邻接表,用来辅助图论的学习很不错。
# -*- encoding: utf-8 -*- from matplotlib import pyplot as plt import networkx as nx N = { ‘a‘: {‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘}, ‘b‘: {‘c‘, ‘e‘}, ‘c‘: {‘d‘}, ‘d‘: {‘e‘}, ‘e‘: {‘f‘}, ‘f‘: {‘c‘, ‘g‘, ‘h‘}, ‘g‘: {‘f‘, ‘h‘}, ‘h‘: {‘f‘, ‘g‘}, } labels = {i: i for i in N.keys()} G = nx.DiGraph(N) pos = nx.spring_layout(G) nx.draw(G, pos, node_color=‘r‘, edge_color=‘b‘) nx.draw_networkx_labels(G, pos, labels, font_size=16, font_color=‘g‘) plt.axis(‘off‘) plt.savefig("labels_and_colors.png") # save as png plt.show() # display
标签:
原文地址:http://www.cnblogs.com/openqt/p/4305530.html