Python 底图:绘制带箭头端盖的大圆
Python basemap: drawgreatcircle with arrow end cap
我正在尝试使用 basemap
中的 drawgreatcircle
函数。有没有办法让线在端盖上有箭头?
我从 matplotlib
文档中看到我可以将 matplotlib 选项作为参数传递。 solid_capstyle
修改端盖,但箭头不是此选项。
编辑:应@swatchai 的要求,我发布了显示我希望工作的代码。它会导致错误,因为 solid_capstyle='arrow' 不是有效选项。不确定这是否满足 "best attempt" 的要求,因为它不起作用。
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')
这是一个变通解决方案。我在大圆的一端画了一个注释(带有空白文本)。注释有一个附带的箭头,需要放置在所需位置。
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()
结果图如下。
我正在尝试使用 basemap
中的 drawgreatcircle
函数。有没有办法让线在端盖上有箭头?
我从 matplotlib
文档中看到我可以将 matplotlib 选项作为参数传递。 solid_capstyle
修改端盖,但箭头不是此选项。
编辑:应@swatchai 的要求,我发布了显示我希望工作的代码。它会导致错误,因为 solid_capstyle='arrow' 不是有效选项。不确定这是否满足 "best attempt" 的要求,因为它不起作用。
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')
这是一个变通解决方案。我在大圆的一端画了一个注释(带有空白文本)。注释有一个附带的箭头,需要放置在所需位置。
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()
结果图如下。