Python底图:带箭头端盖的drawgreatcircle

时间:2017-08-04 17:25:22

标签: matplotlib-basemap

我尝试使用drawgreatcircle中的basemap功能。有没有办法让线条在端盖上有箭头?

我从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')

1 个答案:

答案 0 :(得分:1)

这是一种解决方法。我在大圆的一端绘制了一个注释(带有空白文本)。注释有一个附带的箭头,需要放在所需的位置。

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