Python:使用 NetworkX 和 mplleaflet 的图形
Python: Graph using NetworkX and mplleaflet
我有一个从边缘创建的 networkx 图,例如:
user_id,edges
11011,"[[340, 269], [269, 340]]"
80973,"[[398, 279]]"
608473,"[[69, 28]]"
2139671,"[[382, 27], [27, 285]]"
3945641,"[[120, 422], [422, 217], [217, 340], [340, 340]]"
5820642,"[[458, 442]]"
例子
边缘是用户在集群之间的移动,由他们的集群标签标识,例如 [[340, 269], [269, 340]]
。这表示用户从 cluster 340
到 cluster 269
然后又回到 cluster 340
的移动。这些簇有坐标,存储在另一个文件中,以经纬度的形式存在,比如这些:
cluster_label,latitude,longitude
0,39.18193382,-77.51885109
1,39.18,-77.27
2,39.17917928,-76.6688633
3,39.1782,-77.2617
4,39.1765,-77.1927
是否可以使用 node/cluster 的 lat/long 而不是在抽象 space 中 link 我的图的边缘到它们各自的物理集群 space =46=]的一个图?如果是这样,我该怎么做呢?我想使用 mplleaflet
(如此处所示:http://htmlpreview.github.io/?https://github.com/jwass/mplleaflet/master/examples/readme_example.html)之类的包在地图上绘制此图,或直接绘制到 QGIS/ArcMap.
编辑
我试图将我的带有簇质心坐标的 csv 转换成字典,但是,我 运行 出现了几个错误。主要是 NetwotkXError: Node 0 has no position
和 IndexError: too many indices for array.
以下是我尝试转换为字典然后使用 mplleaflet
.
绘制图表的方式
import csv
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import time
import mplleaflet
g = nx.Graph()
# Set node positions as a dictionary
df = pd.read_csv('G:\Programming Projects\GGS 681\dmv_tweets_20170309_20170314_cluster_centroids.csv', delimiter=',')
df.set_index('cluster_label', inplace=True)
dict_pos = df.to_dict(orient='index')
#print dict_pos
for row in csv.reader(open('G:\Programming Projects\GGS 681\dmv_tweets_20170309_20170314_edges.csv', 'r')):
if '[' in row[1]: #
g.add_edges_from(eval(row[1]))
# Plotting with matplotlib
#nx.draw(g, with_labels=True, alpha=0.15, arrows=True, linewidths=0.01, edge_color='r', node_size=250, node_color='k')
#plt.show()
# Plotting with mplleaflet
fig, ax = plt.subplots()
nx.draw_networkx_nodes(g,pos=dict_pos,node_size=10)
nx.draw_networkx_edges(g,pos=dict_pos,edge_color='gray', alpha=.1)
nx.draw_networkx_labels(g,dict_pos, label_pos =10.3)
mplleaflet.display(fig=ax.figure)
是的,这很容易实现。沿着这条线尝试一些事情。
创建一个字典,其中节点 (cluster_label) 是键,经纬度作为值保存在列表中。我会使用 pd.read_csv() 读取 csv,然后使用 df.to_dict() 创建字典。例如,它应该是这样的:
dic_pos = {u'0': [-77.51885109, 39.18193382],
u'1': [-76.6688633, 39.18],
u'2': [-77.2617, 39.1791792],
u'3': [-77.1927, 39.1782],
.....
然后在地图上绘制图表就像:
import mplleaflet
fig, ax = plt.subplots()
nx.draw_networkx_nodes(GG,pos=dic_pos,node_size=10,node_color='red',edge_color='k',alpha=.5, with_labels=True)
nx.draw_networkx_edges(GG,pos=dic_pos,edge_color='gray', alpha=.1)
nx.draw_networkx_labels(GG,pos=dic_pos, label_pos =10.3)
mplleaflet.display(fig=ax.figure)
如果没有产生预期的结果,请尝试反转纬度和经度。
我有一个从边缘创建的 networkx 图,例如:
user_id,edges
11011,"[[340, 269], [269, 340]]"
80973,"[[398, 279]]"
608473,"[[69, 28]]"
2139671,"[[382, 27], [27, 285]]"
3945641,"[[120, 422], [422, 217], [217, 340], [340, 340]]"
5820642,"[[458, 442]]"
例子
边缘是用户在集群之间的移动,由他们的集群标签标识,例如 [[340, 269], [269, 340]]
。这表示用户从 cluster 340
到 cluster 269
然后又回到 cluster 340
的移动。这些簇有坐标,存储在另一个文件中,以经纬度的形式存在,比如这些:
cluster_label,latitude,longitude
0,39.18193382,-77.51885109
1,39.18,-77.27
2,39.17917928,-76.6688633
3,39.1782,-77.2617
4,39.1765,-77.1927
是否可以使用 node/cluster 的 lat/long 而不是在抽象 space 中 link 我的图的边缘到它们各自的物理集群 space =46=]的一个图?如果是这样,我该怎么做呢?我想使用 mplleaflet
(如此处所示:http://htmlpreview.github.io/?https://github.com/jwass/mplleaflet/master/examples/readme_example.html)之类的包在地图上绘制此图,或直接绘制到 QGIS/ArcMap.
编辑
我试图将我的带有簇质心坐标的 csv 转换成字典,但是,我 运行 出现了几个错误。主要是 NetwotkXError: Node 0 has no position
和 IndexError: too many indices for array.
以下是我尝试转换为字典然后使用 mplleaflet
.
import csv
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import time
import mplleaflet
g = nx.Graph()
# Set node positions as a dictionary
df = pd.read_csv('G:\Programming Projects\GGS 681\dmv_tweets_20170309_20170314_cluster_centroids.csv', delimiter=',')
df.set_index('cluster_label', inplace=True)
dict_pos = df.to_dict(orient='index')
#print dict_pos
for row in csv.reader(open('G:\Programming Projects\GGS 681\dmv_tweets_20170309_20170314_edges.csv', 'r')):
if '[' in row[1]: #
g.add_edges_from(eval(row[1]))
# Plotting with matplotlib
#nx.draw(g, with_labels=True, alpha=0.15, arrows=True, linewidths=0.01, edge_color='r', node_size=250, node_color='k')
#plt.show()
# Plotting with mplleaflet
fig, ax = plt.subplots()
nx.draw_networkx_nodes(g,pos=dict_pos,node_size=10)
nx.draw_networkx_edges(g,pos=dict_pos,edge_color='gray', alpha=.1)
nx.draw_networkx_labels(g,dict_pos, label_pos =10.3)
mplleaflet.display(fig=ax.figure)
是的,这很容易实现。沿着这条线尝试一些事情。 创建一个字典,其中节点 (cluster_label) 是键,经纬度作为值保存在列表中。我会使用 pd.read_csv() 读取 csv,然后使用 df.to_dict() 创建字典。例如,它应该是这样的:
dic_pos = {u'0': [-77.51885109, 39.18193382],
u'1': [-76.6688633, 39.18],
u'2': [-77.2617, 39.1791792],
u'3': [-77.1927, 39.1782],
.....
然后在地图上绘制图表就像:
import mplleaflet
fig, ax = plt.subplots()
nx.draw_networkx_nodes(GG,pos=dic_pos,node_size=10,node_color='red',edge_color='k',alpha=.5, with_labels=True)
nx.draw_networkx_edges(GG,pos=dic_pos,edge_color='gray', alpha=.1)
nx.draw_networkx_labels(GG,pos=dic_pos, label_pos =10.3)
mplleaflet.display(fig=ax.figure)
如果没有产生预期的结果,请尝试反转纬度和经度。