cartopy + networkx:zorder无效

时间:2017-07-20 00:29:14

标签: python matplotlib networkx z-order cartopy

我需要使用cartopy生成地图并在其上绘制一些数据(使用networkx)。我能够做到,但networkx对象在地图后面。我试图使用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()

这是我得到的图像:zorder not working with networkx over cartopy

2 个答案:

答案 0 :(得分:4)

发生了什么事情,zorder=20没有做任何事情;你可以在他们的源代码中看到它被忽略了。 their draw_networkx_edges codenetworkx做的是:

def draw_networkx_edges(G, pos,
    ...
    edge_collection.set_zorder(1)  # edges go behind nodes
    ...
their draw_networkx_nodes code中的

是:

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

现在,解决方案很简单:

  • 如果您将zorder中的add_geometries设置为1,则节点将位于地图前面,因为它是zorder 2.但边缘仍然在地图后面,因为它是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)
    

答案 1 :(得分:2)

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

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

enter image description here