使用python中的networkx模块从原始图形中提取子图

时间:2015-03-06 17:29:59

标签: python networkx

我想从地图图像的图形中提取子图,这样我只得到图的特定边。例如,我想要一个显示从孟买到德里边缘的图表。我应该只获得这个优势,应该使用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() 

0 个答案:

没有答案