2013-07-02 49 views
2

我在繪製圖層時遇到了困難,使用matplotlib-basemap生成的地圖邊界上的多邊形。在下面的例子中,地圖邊界由日期線指定。我嘗試通過指定三角形頂點的座標在日期線上繪製一個三角形。當所有的座標都在地圖內時,這種方式很好,但是如果它們穿過地圖邊界,底圖會執行奇怪的外推,因爲它似乎不知道如何以正確的方式繪製矩形。使用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] 

plotting problem

回答

3

無法解決此問題底圖得到一個簡單的方法。在您的線x,y=m(lons,lats)中,您已將點轉換爲地圖座標,並繪製多邊形只是在這些投影點之間繪製。

你可以嘗試使用Cartopy,它可以做到這一點。 This example可能會有所幫助。