我在绘制使用matplotlib-basemap生成的地图的边界时遇到了困难。在下面的示例中,地图边界由日期线指定。我尝试通过指定三角形顶点的坐标来绘制日期线上的三角形。当所有坐标都在地图中时,这很好用,但如果它们越过地图边界,底图就会执行奇怪的外推,因为它似乎不知道如何以正确的方式绘制矩形。
在我看来,正确的方式意味着三角形被绘制到地图边界,然后在地图的另一边继续绘制。
下面是一个最小的代码示例和一个说明一般问题的图。任何如何解决这个问题的想法都是非常受欢迎的。
from mpl_toolkits.basemap import Basemap
import matplotlib.pylab as plt
import numpy as np
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib as mpl
from matplotlib.collections import PatchCollection
![plt.close('all')
Path = mpath.Path
fig=plt.figure(); ax=fig.add_subplot(121); ax1=fig.add_subplot(122)
def do_plot(ax,lons,lats,title):
patches=\[\]
m = Basemap(projection='robin', resolution='c',lon_0=0.,ax=ax) #todo: how to make it properly work for other projections ???
m.drawmapboundary(fill_color='grey')
m.drawcoastlines()
#--- generate first sample with no problem
x,y=m(lons,lats)
verts = np.asarray(\[x,y\]).T
codes = \[Path.MOVETO,Path.LINETO,Path.LINETO\]
patches.append(mpatches.PathPatch(mpath.Path(verts, codes,closed=True)))
#--- generate collection
cmap = plt.cm.get_cmap('jet', 50); norm = mpl.colors.Normalize(vmin=None, vmax=None) #colorbar mapping
collection = PatchCollection(patches, cmap=cmap,norm=norm, alpha=1.,match_original=False) #construct library of all objects
colors = np.asarray(np.random.random(len(patches)))
collection.set_array(np.array(colors)) #assign data values here
#--- do actual plotting
im=m.ax.add_collection(collection)
ax.set_title(title)
do_plot(ax,\[-10.,0.,20.\],\[30.,50.,20.\],'This works')
do_plot(ax1,\[170,180,-175\],\[30.,50.,20.\],'... and here is the boundary problem')
plt.show()][1]

发布于 2013-07-13 10:51:31
使用底图不能以简单的方式解决这个问题。在您的线x,y=m(lons,lats)中,您已经将点转换为地图坐标,并且绘制多边形只是在这些投影点之间绘制。
您可以尝试使用Cartopy,它可以做到这一点。This example可能会有所帮助。
https://stackoverflow.com/questions/17423692
复制相似问题