I'm attempting this tutorial: "Networks on Maps (with Python)"。在原作者的帮助下,我已经完成了大部分工作。它最初是在Python 2.7中创建的,我使用的是3.5。更改包括graphml格式的数据(不是pickle),插入缺少的参数,以及指定G = nx.Graph()。
我现在陷入了错误:
NameError: name 'part' is not defined.
我有点不确定是否存在2.7-3.5的冲突,如果缺少一个功能?
以下是代码:
import matplotlib.pyplot as plt
import networkx as nx
from mpl_toolkits.basemap import Basemap
import difflib
m = Basemap(projection='robin',lon_0=0,resolution='l')
m.drawcountries(linewidth = 0.5)
m.fillcontinents(color='white',lake_color='white')
m.drawcoastlines(linewidth=0.5)
# load geographic coordinate system for countries
import csv
country = [row[0].strip() for row in csv.reader(open('LonLat.csv'), delimiter=';')] # clear spaces
lat = [float(row[1]) for row in csv.reader(open('LonLat.csv'), delimiter=';')]
lon = [float(row[2]) for row in csv.reader(open('LonLat.csv'), delimiter=';')]
G=nx.Graph()
# define position in basemap
position = {}
for i in range(0, len(country)):
position[country[i]] = m(lon[i], lat[i])
def similar(landstring, country):
l = difflib.get_close_matches(landstring, country, 1)
return l[0]
pos = dict((land, position[similar(land, country)]) for land in G.nodes())
nx.draw_networkx_nodes(G, pos, nodelist = [**key for key in part if part[key] == 0**],
node_size = [deg_weight[s]*10 for s in part if part[s] == 0],
node_color = 'red', node_shape='^', alpha=0.8)
nx.draw_networkx_nodes(G, pos, nodelist = [key for key in part if part[key] == 1],
node_size = [deg_weight[s]*20 for s in part if part[s] == 1],
node_color = 'black', node_shape='d')
nx.draw_networkx_nodes(G, pos, nodelist = [key for key in part if part[key] == 2],
node_size = [deg_weight[s]*10 for s in part if part[s] == 2],
node_color = 'green', node_shape='o')
nx.draw_networkx_nodes(G, pos, nodelist = [key for key in part if part[key] == 3],
node_size = [deg_weight[s]*10 for s in part if part[s] == 3],
node_color = 'blue', alpha=0.8)
nx.draw_networkx_edges(G, pos, color='grey', width = 0.75, alpha=0.2)
plt.show()
感觉就像这个答案(How to retrieve or iterate over edge keys in python networkx MultiDiGraph),这个答案(Python Tulpe Key For Dict Partial Lookup),这个答案(How to access keys in nodes and edges in NetworkX MultiDiGraph)正指向正确的方向,但我做得不够一个链接。
非常感谢任何输入。我很乐意让它充分发挥作用。
答案 0 :(得分:0)
以下代码几乎完全有效。还有一些小问题,例如:地图显示在边缘和节点“上方”,“deg_weight”未定义,但它主要起作用。如果我完全修复,将发布。在这里发帖给任何有兴趣的人。感谢Raphael Heiberger博士的慷慨援助。
#import sys
#sys.version
#'3.5.1 |Anaconda 4.0.0 (64-bit)...
# import modules
import matplotlib.pyplot as plt
import networkx as nx
from mpl_toolkits.basemap import Basemap
import csv
import difflib
import community # https://bitbucket.org/taynaud/python-louvain
# define map
m = Basemap(projection='robin',lon_0=0,resolution='l')
m.drawcountries(linewidth = 0.5)
m.fillcontinents(color='white',lake_color='white')
m.drawcoastlines(linewidth=0.5)
# load geographic coordinates for countries
country = [row[0].strip() for row in csv.reader(open('LonLat.csv'), delimiter=';')] # clear spaces
lat = [float(row[1]) for row in csv.reader(open('LonLat.csv'), delimiter=';')]
lon = [float(row[2]) for row in csv.reader(open('LonLat.csv'), delimiter=';')]
# load data
G = nx.read_graphml('data.graphml')
# define position in basemap
position = {}
for i in range(0, len(country)):
position[country[i]] = m(lon[i], lat[i])
def similar(landstring, country):
l = difflib.get_close_matches(landstring, country, 1)
return l[0]
pos = dict((land, position[similar(land, country)]) for land in G.nodes())
part = community.best_partition(G)
# draw graph
nx.draw_networkx_nodes(G, pos, nodelist = [key for key in part if part[key] == 0],
node_size = [deg_weight[s]*10 for s in part if part[s] == 0],
node_color = 'red', node_shape='^', alpha=0.8)
nx.draw_networkx_nodes(G, pos, nodelist = [key for key in part if part[key] == 1],
node_size = [deg_weight[s]*20 for s in part if part[s] == 1],
node_color = 'black', node_shape='d')
nx.draw_networkx_nodes(G, pos, nodelist = [key for key in part if part[key] == 2],
node_size = [deg_weight[s]*10 for s in part if part[s] == 2],
node_color = 'green', node_shape='o')
nx.draw_networkx_nodes(G, pos, nodelist = [key for key in part if part[key] == 3],
node_size = [deg_weight[s]*10 for s in part if part[s] == 3],
node_color = 'blue', alpha=0.8)
nx.draw_networkx_edges(G, pos, color='grey', width = 0.75, alpha=0.2)
#print/generate plot
plt.show()