检索部分密钥 'key for key in part...' - Networkx
Retrieving partial keys 'key for key in part...' - Networkx
我正在尝试本教程:"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), this answer (Python Tulpe Key For Dict Partial Lookup), and this answer (How to access keys in nodes and edges in NetworkX MultiDiGraph) 指向了正确的方向,但我无法充分理解 link。
如有任何意见,我们将不胜感激。我很想让它充分发挥作用。
以下代码几乎完全有效。一些小问题仍然存在,例如地图显示 "above" 边缘和节点,'deg_weight' 未定义,但它主要工作。 post 如果我有时间完全修复。在这里张贴给任何有兴趣的人。感谢 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()
我正在尝试本教程:"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), this answer (Python Tulpe Key For Dict Partial Lookup), and this answer (How to access keys in nodes and edges in NetworkX MultiDiGraph) 指向了正确的方向,但我无法充分理解 link。
如有任何意见,我们将不胜感激。我很想让它充分发挥作用。
以下代码几乎完全有效。一些小问题仍然存在,例如地图显示 "above" 边缘和节点,'deg_weight' 未定义,但它主要工作。 post 如果我有时间完全修复。在这里张贴给任何有兴趣的人。感谢 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()