matplotlib cartopy + networkx:zorder不工作

qeeaahzv  于 2023-05-07  发布在  其他
关注(0)|答案(3)|浏览(283)

我需要使用cartopy生成一个Map,并在其上绘制一些数据(使用networkx)。我能够做到这一点,但networkx对象是后面的Map。我试图使用zorder强制层的顺序,但是...它不起作用:(
我唯一的想法是在cartopy几何体中添加一些透明度,但它看起来一点也不好看。(在这个例子中,它看起来并不那么糟糕,但在我的整个数据中,它看起来很糟糕)
有没有什么办法强制执行命令?
下面是我的代码:

import cartopy.crs as ccrs
from cartopy.io import shapereader as shpreader
import matplotlib.pyplot as plt
import networkx as nx

paises = ['Portugal', 'France', 'Canada', 'Brazil', 'Kazakhstan']
cidades = ['Aveiro', 'Ust-Kamenogorsk', 'Manaus']
links = [('Aveiro', 'Ust-Kamenogorsk'), 
         ('Manaus', 'Ust-Kamenogorsk'),
         ('Aveiro', 'Manaus')]
position = {'Aveiro': (-8.65, 40.6),
            'Manaus': (-60.0, -3.1),
            'Ust-Kamenogorsk': (82.6, 49.97)}

# map using cartopy:
shapename = 'admin_0_countries'
countries_shp = shpreader.natural_earth(resolution='110m',
                                    category='cultural', name=shapename)

ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0.0, globe=None))
ax.set_global()

for country in shpreader.Reader(countries_shp).records():    
    nome = country.attributes['name_long']
    if nome in paises:
        i = paises.index(nome)
        artist = ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                           facecolor='yellow',
                           #alpha=0.5,
                           zorder=10)
    else:
        artist = ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                           facecolor='0.9',
                           zorder=10)

# add some data over the cartopy map (using networkx):                              
G = nx.Graph()
G.add_nodes_from(cidades)
G.add_edges_from(links)

nx.draw_networkx_nodes(G, position, node_size=20, nodelist=cidades, zorder=20)
edges=nx.draw_networkx_edges(G, position, edgelist=links, zorder=20)

plt.show()

这是我得到的图像:

wa7juj8i

wa7juj8i1#

发生的情况是您的zorder=20不做任何事情;它被忽略了,你可以在他们的源代码中看到。networkx在其draw_networkx_edges代码中所做的是:

def draw_networkx_edges(G, pos,
    ...
    edge_collection.set_zorder(1)  # edges go behind nodes
    ...

draw_networkx_nodes代码是:

def draw_networkx_nodes(G, pos,
    ...
    node_collection.set_zorder(2)
    ...

现在,解决方案很简单:

  • 如果将add_geometries中的zorder设置为1,则节点将位于map的前面,因为它是zorder 2。但是边仍然在Map后面,因为它是zorder 1。
  • 现在真实的更好的解决方案是首先获取node_collection和edge_collection:
nodes = nx.draw_networkx_nodes(G, position, node_size=20, nodelist=cidades)
edges = nx.draw_networkx_edges(G, position, edgelist=links)

然后set_zorder用于节点和边:

nodes.set_zorder(20)
edges.set_zorder(20)
huwehgph

huwehgph2#

如果将添加到贴图的形状的zorder设置为0,则一切都将按预期工作。

ax.add_geometries(..., zorder=0)

vd8tlhqk

vd8tlhqk3#

这里是另一种方法

# Sort the nodes by degree
sorted_nodes = sorted(G.nodes, key=lambda x: G.degree(x))

# Draw edges
nx.draw_networkx_edges(G, pos, alpha=0.4, edge_color='gray', arrows=False, zorder=0)

# Draw nodes and labels with zorder based on the degree
for node in sorted_nodes:
    nx.draw_networkx_nodes(G, pos, nodelist=[node], node_color='skyblue', node_size=node_sizes[node], zorder=G.degree[node], ax=ax)
    nx.draw_networkx_labels(G, pos, labels={node: node}, font_size=9, font_color='black', bbox=def_bbox_props, zorder=G.degree[node] + 1, ax=ax)

相关问题