3

嗨,我正在尝试使用 Basemap 中的 Greatcircle 函数(matplot lib 的一部分)将 IP 地址图映射到世界地图上,但每次我连接跨越太平洋的两个点(即美国西海岸的某个地方和澳大利亚)有一条水平线出现在我的情节中。

我知道这是因为这个问题:

Note Cannot handle situations in which the great circle intersects the edge of the map projection domain, and then re-enters the domain. (http://matplotlib.org/basemap/api/basemap_api.html#mpl_toolkits.basemap.Basemap.drawgreatcircle)

只是想知道是否有人知道如何修复它或知道另一个没有这个问题的包。谢谢!

4

2 回答 2

8

要在 Basemap 中针对特定情况解决此问题,您可以检查结果路径中 x 顶点的差异。从那里,我们可以将坏路分成两部分。我已经将自己的示例与底图放在一起:

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt


m = Basemap(projection='cyl', lon_0=0, resolution='c')

m.fillcontinents(color='coral',lake_color='aqua')
m.drawmapboundary(fill_color='aqua')

places = {'Mexico City': (19.05, -99.366667),
          'London': (51.507222, -0.1275),
          'Sydney': (-33.859972, 151.211111),
          'Cape Town': (-33.925278, 18.423889),
          'Delhi': (28.61, 77.23),
          }

network = [
           ('London', 'Delhi'),
           ('Mexico City', 'Sydney'),
           ]


for source, target in network:
    lat1, lon1 = places[source]
    lat2, lon2 = places[target]
    line, = m.drawgreatcircle(lon1, lat1, lon2, lat2, lw=3)

    p = line.get_path()
    # find the index which crosses the dateline (the delta is large)
    cut_point = np.where(np.abs(np.diff(p.vertices[:, 0])) > 200)[0]
    if cut_point:
        cut_point = cut_point[0]

        # create new vertices with a nan inbetween and set those as the path's vertices
        new_verts = np.concatenate(
                                   [p.vertices[:cut_point, :], 
                                    [[np.nan, np.nan]], 
                                    p.vertices[cut_point+1:, :]]
                                   )
        p.codes = None
        p.vertices = new_verts


plt.show()

结果:

示例输出

这不是一个通用的解决方案,只有当你有纬度和经度时才有效。解决一般问题要困难得多,并且是cartopy 的重点 (http://scitools.org.uk/cartopy/docs/latest/)。在接下来的几天里,我将在 cartopy 中发布一个完全相同的情节的例子。

高温高压

于 2013-01-04T09:40:11.840 回答
0

另一个没有这个问题的包:cartopy(目前尚未公布)。此处找到的示例是相关的。

高温高压

于 2013-01-04T08:40:28.477 回答