Python Network(三)案例(无向图,有向图,权重,点线分类与大小粗细)

本文是Python Network系列的实战篇,详细介绍了如何创建无向图、有向图,以及如何添加权重。通过数据源数组和外部数据引入构建图,并探讨了点线分类及点大小控制的方法。文中还提到了根据点的邻近数调整点大小的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有关Python Network前面已经写过两篇基础入门文章
Python Network(一)
Python Network(二)
本文主要是代码实战的case,主要看一些小技巧的地方

1. 无向图

1.1 无向图-数据来源数组

import networkx as nx
# 创建空的网格
G=nx.Graph()
# 添加节点
G.add_node('JFK')
G.add_nodes_from(['SFO','LAX','ATL','FLO','DFW','HNL'])
# G.number_of_nodes()  # 查看节点数,输出结果7

# 添加连线
G.add_edges_from([('JFK','SFO'),('JFK','LAX'),('LAX','ATL'),('FLO','ATL'),('ATL','JFK'),('FLO','JFK'),('DFW','HNL')])
G.add_edges_from([('OKC','DFW'),('OGG','DFW'),('OGG','LAX')])
# 绘制网络图
# nx.draw(G,pos=nx.circular_layout(G),with_labels=True)
nx.draw_networkx(G,pos=nx.circular_layout(G),with_labels=True,alpha=0.5,node_color='yellow',node_shape='s',
                 linewidths=4,
                 width=2,edge_color='blue',style='--',
                 font_size=15,font_color='blue',font_family='SimHei')
             
# pos选用圆形样式,with_labels=True在节点上绘制标签,alpha=0.5节点透明度
#linewidths=4节点边框宽度为4,node_color='yellow'节点颜色设为黄色,node_shape='s'节点的形状设为正方形
# width=2边的线宽2,edge_color='blue'设置边的颜色,style='--'边的线样式,
# font_size=15设置标签字号大小,font_color='blue'设置标签字体颜色,font_family='SimHei'设置标签字体

在这里插入图片描述

1.2 无向图-数据来源外部引入数据

import pandas as pd
import networkx as nx

NXdata=pd.read_excel('file_name',index_col=0)
nf=nx.from_pandas_adjacency(NXdata) # 从外部引入数据,也可以是dataframe格式

nx.draw_networkx(nf,pos=nx.circular_layout(nf),with_labels=True,alpha=0.5)

在这里插入图片描述

2. 有向图

#step1:创建空图
import networkx as nx
G=nx.DiGraph() #创建什么都没有的空图

#step2:给上述空图加边
start=[1,3,5,7]#边的起点列表
to=[2,4,6,8]#边的终点列表
for j in range(len(start)):
    G.add_edge(start[j], to[j]) 
nx.draw(G,with_labels=True)
import matplotlib.pyplot as plt
plt.show()
# 最后两句其实不加,也没什么

在这里插入图片描述

3. 带权重

3.1 带权重无向图

test1

#step1:创建空图
import networkx as nx
G=nx.Graph() #创建什么都没有的空图

#step2:给上述空图加边
start=[1,3,5,7,9,1]#边的起点列表
to=[2,4,6,8,10,3]#边的终点列表
value=[0.1,1,10,15,5,6] #边的权重列表

for j in range(0, len(start)):
    G.add_weighted_edges_from([(start[j], to[j], value[j])])  # 边的起点,终点,权重
#step3:规定图的输出:边的粗细、节点的摆放位置等
nx.draw(G, with_labels=True,pos=nx.circular_layout(G),
        width=[float(v['weight']) for (r, c, v) in G.edges(data=True)])

在这里插入图片描述

test2

import matplotlib.pyplot as plt
import networkx as nx
 
G = nx.Graph()
 
G.add_edge('a', 'b', weight=0.6)
G.add_edge('a', 'c', weight=0.2)
G.add_edge('c', 'd', weight
Mastering Python Networking English | 2017 | ISBN-10: 1784397008 | 446 pages | PDF + EPUB + MOBI (conv) | 9.76 Mb Key Features Build the skills to perform all networking tasks using Python with ease Use Python for Network Device Automation, DevOps, and Software Defined Networking Practical guide to networking with Python Book Description You will begin with a review of the TCP/IP Protocol Suite and to refresh the core elements of the Python language. Next you will start using Python to automate network devices and achieve all that you want from your network. You will then move to using Python for DevOps where you will be securing networks, monitoring networks, and building network services. In the last module, you will use Python for SDN, where you will use Python with OpenFlow, OpenStack , OpenDayLight, and NFV. Finally you will use everything you have learned so far in this book to construct a Python-based migration plan to go from legacy to a scalable SDN-based network. By the end of the book you will have all the skills required to perform complex network-related tasks with Python What you will learn Review of all the fundamentals of Python and TCP/IP suite Use Python to execute commands when the device does not support the API or programmatic interaction with the device Implement automation techniques by integrating Python with Cisco, Juniper, and and Arista EAPI Integrate Ansible using Python to control Cisco, Juniper, and Arista networks Achieve Network security with Python Build high-performing web services with Python Construct a Python-based migration plan from legacy to a scalable SDN-based network. Download from icerbox.com
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值