python中networkx包学习——画网络图像
来源:互联网 发布:安卓手机长截图软件 编辑:程序博客网 时间:2024/05/21 06:33
简单示例
import networkx as nximport matplotlib.pyplot as pltdef draw_graph(graph): # extract nodes from graph nodes = set([n1 for n1, n2 in graph] + [n2 for n1, n2 in graph]) # create networkx graph G=nx.Graph() # add nodes for node in nodes: G.add_node(node) # add edges for edge in graph: G.add_edge(edge[0], edge[1]) # draw graph pos = nx.shell_layout(G) nx.draw(G, pos) # show graph plt.show()# draw examplegraph = [(20, 21),(21, 22),(22, 23), (23, 24),(24, 25), (25, 20)]draw_graph(graph)
运行结果:
————————————————————————————————————————————————————————————————————————
复杂示例
import networkx as nximport matplotlib.pyplot as pltdef draw_graph(graph, labels=None, graph_layout='shell', node_size=1600, node_color='blue', node_alpha=0.3, node_text_size=12, edge_color='blue', edge_alpha=0.3, edge_tickness=1, edge_text_pos=0.3, text_font='sans-serif'): # create networkx graph G=nx.Graph() # add edges for edge in graph: G.add_edge(edge[0], edge[1]) # these are different layouts for the network you may try # shell seems to work best if graph_layout == 'spring': graph_pos=nx.spring_layout(G) elif graph_layout == 'spectral': graph_pos=nx.spectral_layout(G) elif graph_layout == 'random': graph_pos=nx.random_layout(G) else: graph_pos=nx.shell_layout(G) # draw graph nx.draw_networkx_nodes(G,graph_pos,node_size=node_size, alpha=node_alpha, node_color=node_color) nx.draw_networkx_edges(G,graph_pos,width=edge_tickness, alpha=edge_alpha,edge_color=edge_color) nx.draw_networkx_labels(G, graph_pos,font_size=node_text_size, font_family=text_font) if labels is None: labels = range(len(graph)) edge_labels = dict(zip(graph, labels)) nx.draw_networkx_edge_labels(G, graph_pos, edge_labels=edge_labels, label_pos=edge_text_pos) # show graph plt.show()graph = [(0, 1), (1, 5), (1, 7), (4, 5), (4, 8), (1, 6), (3, 7), (5, 9), (2, 4), (0, 4), (2, 5), (3, 6), (8, 9)]# you may name your edge labelslabels = map(chr, range(65, 65+len(graph)))#draw_graph(graph, labels)# if edge labels is not specified, numeric labels (0, 1, 2...) will be useddraw_graph(graph)
运行结果:
参考
更多详细介绍可参考网站1
阅读全文
0 0
- python中networkx包学习——画网络图像
- python中的networkx包学习——简单的网络画图入门
- python中的networkx包学习——简单的网络画图入门
- python中networkx包学习——最短路径函数shortest_path及shorest_path_length
- python包NetworkX学习——最短路径dijkstra_path和dijkstra_path_length
- Python NetworkX/Community包进行网络划分和可视化
- python networkx学习
- python networkx学习
- Python networkx 实现网络流
- 绘制网络分面图—matplotlib,networkx
- python—networkx:各个布局的源代码
- NetworkX学习笔记——基本功能使用
- python下的复杂网络编程包networkx、matplotlib、numpy安装及报错解决方案
- NetworkX学习笔记-5-NetworkX中怎样对多个网络赋属性,并根据属性排序
- centos安装python-pip和networkx包
- 最短路径(Python)networkx 包
- Python Networkx 包常用的方法函数
- python—networkx:在一张图中画出多个子图
- 架构师成长
- mysql查询语句
- JavaScript(2)网页时钟,String,数组
- 小程序学习(二)
- 读取XML文件之DOM官方方法
- python中networkx包学习——画网络图像
- 项目中遇到的问题
- PhpApplication2--智慧--php-webview
- 架构师之路(二)
- 安装myeclipse8.5
- 冒泡排序介绍与分析
- UVA-816 Abbott's Revenge (bfs)
- 设计模式之抽象工厂模式
- json字符串与json对象的相互转换