python networkx如何抽取子图_在python中使用networkx模块从原始图中提取子图

本文展示了如何利用Python的networkx模块从地图图像的图中提取特定的子图,例如从孟买到德里的边。通过创建Graph对象并添加边权重,然后绘制节点和边,最终保存为weighted_graph.png。

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

我想从地图图像的图中提取一个子图,这样我就只得到图的特定边。例如,我想要一个图表显示从孟买到德里的边缘。我应该只得到边缘,它应该使用networkx模块来完成。在import matplotlib.pyplot as plt

import PIL

from PIL import Image

import numpy

#try:

#import matplotlib.pyplot as plt

#from scipy.interpolate import spline

#except:

#raise

import networkx as nx

imarray = numpy.asarray(Image.open('C:\Python34\Lib\site-packages\PIL\India_locator_map_blank.jpg'))

G=nx.Graph()

a="Mumbai"

b="Jamnagar"

c="Bhuj"

d="Ahmedabad"

e="Udaipur"

f="Delhi"

g="Jaipur"

h="Indore"

i="Bhopal"

j="Agra"

k="Gwalior"

l="Lucknow"

m="Allahabad"

n="Varanasi"

o="Kolkata"

p="Nagpur"

q="Raipur"

r="Aurangabad"

s="Bhubaneshwar"

t="Vishakhapatanam"

u="Hydrabad"

v="Pune"

w="Bengaluru"

x="Coimbatore"

y="Kochi"

z="kozhikode"

a1="Goa"

b1="Mangalore"

c1="Thiruvananthapuram"

G.add_edge(a,b,weight=0.5)

G.add_edge(a,c,weight=0.5)

G.add_edge(a,d,weight=0.5)

G.add_edge(a,e,weight=0.5)

G.add_edge(a,f,weight=0.5)

G.add_edge(a,g,weight=0.5)

G.add_edge(a,h,weight=0.5)

G.add_edge(a,i,weight=0.5)

G.add_edge(a,j,weight=0.5)

G.add_edge(a,k,weight=0.5)

G.add_edge(a,l,weight=0.5)

G.add_edge(a,m,weight=0.5)

G.add_edge(a,n,weight=0.5)

G.add_edge(a,o,weight=0.5)

G.add_edge(a,p,weight=0.5)

G.add_edge(a,q,weight=0.5)

G.add_edge(a,r,weight=0.5)

G.add_edge(a,s,weight=0.5)

G.add_edge(a,t,weight=0.5)

G.add_edge(a,u,weight=0.5)

G.add_edge(a,v,weight=0.5)

G.add_edge(a,w,weight=0.5)

G.add_edge(a,x,weight=0.5)

G.add_edge(a,y,weight=0.5)

G.add_edge(a,z,weight=0.5)

G.add_edge(a,a1,weight=0.5)

G.add_edge(a,b1,weight=0.5)

G.add_edge(a,c1,weight=0.5)

pos = {'Mumbai':(0.23159,0.43215), # positions for all nodes

'Jamnagar':(-5,20),

'Bhuj':(-7,30),

'Ahmedabad':(-1,25),

'Udaipur':(3,15),

'Delhi':(10,25),

'Jaipur':(5,20),

'Indore':(7,8),

'Bhopal':(11,9),

'Agra':(12,19),

'Gwalior':(13,17),

'Lucknow':(15,20),

'Allahabad':(17,15),

'Varanasi':(18,18),

'Kolkata':(30,10),

'Nagpur':(14,6),

'Raipur':(16,7),

'Aurangabad':(5,5),

'Bhubaneshwar':(25,6),

'Vishakhapatanam':(22,-1),

'Hydrabad':(12,-8),

'Pune':(4,-2),

'Bengaluru':(9,-15),

'Coimbatore':(11,-20),

'Kochi':(6,-25),

'kozhikode':(4,-18),

'Goa':(2,-9),

'Mangalore':(4,-15),

'Thiruvananthapuram':(7,-30)

}

#pos=nx.spring_layout(G) # positions for all nodes

# nodes

nx.draw_networkx_nodes(G,pos,node_size=500, node_color="pink")

# edges

nx.draw_networkx_edges(G,pos,

width=1,alpha=0.5,edge_color='black')

# labels

#nx.draw_networkx_labels(G,pos,font_size=14,font_family='sans-serif')

#nx.draw_networkx_edge_labels(G,pos,

#{

# (c,b):"least distance", (c,a):"most distance", (c,d):"<9000km"

#}

#)

plt.imshow(imarray, extent=[0,1,0,1])

plt.axis('off')

plt.savefig("weighted_graph.png") # save as png

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值