仅绘制Osmnx网络的牢固连接的组件

时间:2020-06-23 10:13:44

标签: python networkx osmnx

我有以下网络:

SELECT 
    IdTable2,
    Table2Two,
    Table2Three 
FROM 
    AnyTable2 
WHERE 
    Table2Three = Eval("[Forms].[YourForm].[cboFilter].Column(2)");

如何绘制新的G_1网络?

2 个答案:

答案 0 :(得分:2)

请注意,示例中的G_1是一组已连接的组件的列表。您将需要生成诱导子图以进行绘制。 说我们有图:

enter image description here

具有牢固连接的组件:

list(nx.strongly_connected_components(G))
# [{3, 8, 9}, {1, 2, 4, 5}, {6, 7}]

您可以使用nx.subgraph从连接的组件生成感应子图,并从那里绘制生成的图。在这里,我使用子图将它们可视化为网格:

from itertools import zip_longest
from matplotlib import pyplot as plt
from math import ceil

comps = list(nx.strongly_connected_components(G))
n_cols = 2

fig, axes = plt.subplots(nrows=int(ceil(len(comps)/n_cols)), 
                         ncols=n_cols, 
                         figsize=(15,8))

for comp, ax in zip_longest(comps, axes.flatten()):
    if comp is None:
        plt.box(False)
        plt.axis('off')
        continue
    G_sub = G.subgraph(comp)
    nx.draw(G_sub, with_labels=True, node_color='lightblue', node_size=500, ax=ax)

enter image description here

答案 1 :(得分:2)

根据最终目标,可以使用get_largest_componentinduce_subgraph函数对OSMnx进行此操作。我在这里介绍了两种选择:

import networkx as nx
import osmnx as ox
ox.config(log_console=True, use_cache=True)
city = 'Portugal, Lisbon'
G = ox.graph_from_place(city, network_type='drive')
print(len(G)) #9699

# OPTION 1: if you only want the largest strongly connected component
Gc = ox.utils_graph.get_largest_component(G, strongly=True)
print(len(Gc)) #9503
fig, ax = ox.plot_graph(Gc, node_size=0)

# OPTION 2: if you want all the strongly connected components
# first flatten the list of sets of nodes
nodes = [y for x in nx.strongly_connected_components(G) if len(x) > 1 for y in x]
Gc = ox.utils_graph.induce_subgraph(G, nodes)
print(len(Gc)) #9551
fig, ax = ox.plot_graph(Gc, node_size=0)

编辑:@yatu很好地说明了紧密连接的组件的大小。我在上面编辑了一行,只保留了节点数超过1的牢固连接的组件。

相关问题