2017-08-04 99 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