2017-08-04 114 views
1

我试图在basemap中使用drawgreatcircle函数。有没有办法让线条在端盖上有箭头?Python底图:带箭头端盖的drawgreatcircle

我从matplotlib文档中看到我可以将matplotlib选项作为参数传递。 solid_capstyle修改了端盖,但箭头不是这个选项。

编辑:根据@swatchai的要求,我发布了显示我希望工作的代码。它会导致错误,因为solid_capstyle ='arrow'不是有效的选项。不确定这是否满足“最佳尝试”的要求,因为它不起作用。

from mpl_toolkits.basemap import Basemap 
import numpy as np 
import matplotlib.pyplot as plt 
# create new figure, axes instances. 
fig=plt.figure() 
ax=fig.add_axes([0.1,0.1,0.8,0.8]) 
# setup mercator map projection. 
m = Basemap(llcrnrlon=-100.,llcrnrlat=20.,urcrnrlon=20.,urcrnrlat=60.,\ 
      rsphere=(6378137.00,6356752.3142),\ 
      resolution='l',projection='merc',\ 
      lat_0=40.,lon_0=-20.,lat_ts=20.) 
# nylat, nylon are lat/lon of New York 
nylat = 40.78; nylon = -73.98 
# lonlat, lonlon are lat/lon of London. 
lonlat = 51.53; lonlon = 0.08 
# draw great circle route between NY and London 
m.drawgreatcircle(nylon,nylat,lonlon,lonlat,linewidth=2,color='b', solid_capstyle='arrow') 
m.drawcoastlines() 
m.fillcontinents() 
# draw parallels 
m.drawparallels(np.arange(10,90,20),labels=[1,1,0,1]) 
# draw meridians 
m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1]) 
ax.set_title('Great Circle from New York to London') 
+0

请张贴您的'最佳尝试'代码。这将增加更多获得答案的机会。我脑海里已经有了一个。 – swatchai

+0

添加代码。不确定这足以提高答案的可能性。 – AnonymousCowherd

回答

0

这是一种解决方法。我在大圆圈的一端绘制了一个注释(带有空白文本)。该注释具有需要放置在期望位置的伴随箭头。

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

# create new figure, axes instances. 
fig=plt.figure(figsize=(12,7)) 
ax=fig.add_axes([0.1,0.1,0.8,0.8]) 

# setup mercator map projection. 
m = Basemap(llcrnrlon=-100.,llcrnrlat=20.,urcrnrlon=20.,urcrnrlat=60.,\ 
      rsphere=(6378137.00,6356752.3142),\ 
      resolution='l',projection='merc',\ 
      lat_0=40.,lon_0=-20.,lat_ts=20.) 
# nylat, nylon are lat/lon of New York 
nylat = 40.78; nylon = -73.98 
# lonlat, lonlon are lat/lon of London. 
lonlat = 51.53; lonlon = 0.08 

# draw great circle route between NY and London 
# m.drawgreatcircle(nylon,nylat,lonlon,lonlat,linewidth=2,color='b', solid_capstyle='arrow') 

# ============= 
# begin my code 
# ------------- 

# grab the great circle, assign a variable for it 
gcline, = m.drawgreatcircle(nylon,nylat,lonlon,lonlat,linewidth=2,color='b') 
path = gcline.get_path() # get path from the great circle 

head = m(lonlon,lonlat)    # get location of arrow's head (at London) 
tail = path.vertices[-len(path)/6] # get location of arrow's tail 

# draw annotation with arrow in 'red' color 
# blank text is specified, because we need the arrow only 
# adjust facecolor and other arrow properties as needed 
ax.annotate('', 
      xy=(head[0], head[1]), 
      xycoords='data', 
      xytext=(tail[0], tail[1]), 
      textcoords='data', 
      size=22, 
      arrowprops=dict(headwidth=15, \ 
          headlength=25, \ 
          facecolor="red", \ 
          edgecolor="none", \ 
          connectionstyle="arc3, rad=0.001")) 
# ----------- 
# end my code 
# =========== 

m.drawcoastlines() 
m.fillcontinents() 
# draw parallels 
m.drawparallels(np.arange(10,90,20),labels=[1,1,0,1]) 
# draw meridians 
m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1]) 
ax.set_title('Great Circle from New York to London') 

plt.show() 

结果如下。 enter image description here