简体   繁体   中英

How to draw edges as 3D arcs on a geographic map using python?

I want to create a network map similar to

在此处输入图片说明

in python. So far I am able to draw it on 2D. The density of edges is making the graph untidy. Plotting the edges in 3-D would enhance the beauty and make it more appealing. Any help would be highly appreciated. Thanks in advance

In principle, what you want to do is possible with Basemap . You can draw a Basemap in a 3D axis following this tutorial and then just plot your arcs on top of that. However, if you want to colour different countries the way your example figure implies, you have to start dealing with shapefiles (see for instance here or here ). Anyway, below an example code that plots the Basemap and the arcs in a 3D plot. The part that plots the Basemap in the 3D axes is taken directly from the tutorial I linked above.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap
from matplotlib.collections import PolyCollection
import numpy as np

m = Basemap()

fig = plt.figure()
ax = Axes3D(fig)

ax.azim = 270
ax.elev = 50
ax.dist = 8

ax.add_collection3d(m.drawcoastlines(linewidth=0.25))
ax.add_collection3d(m.drawcountries(linewidth=0.35))

polys = []
for polygon in m.landpolygons:
    polys.append(polygon.get_coords())


lc = PolyCollection(polys, edgecolor='black',facecolor='#DDDDDD', closed=False)
ax.add_collection3d(lc)

def plot_arc(ax,p1,p2,height,N=100):
    x = np.linspace(p1[0],p2[0],N)
    y = np.linspace(p1[1],p2[1],N)
    z = (1-np.linspace(-1,1,N)**2)*height
    ax.plot(x,y,z)

plot_arc(ax,(-70,0),(80,50),1)
plot_arc(ax,(80,50),(125,-25),1)
plot_arc(ax,(125,-25),(-70,0),1)

plt.show()

Here the result of the example code:

以上代码的结果

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM