2017-05-12 8 views
4

ニュージーランドのプロットを作成したいと思います。しかし、私が画像を生成するために使用したシェイプファイルは、土地の端を越えて海に広がっています。つまり、ポリゴンに色付けすると、海の色付け部分も終わることになります。希望の応答ではありません!Matplotlib Basemapのmaskoceans()をポリゴンパッチに適用する方法

私が使用していたコードは次のようになります。下の画像を生成

from mpl_toolkits.basemap import Basemap 
from matplotlib.patches import Polygon 

plt.figure(figsize=(15,15)) 
lonmax = 180 
lonmin = 165 
latmax = -33 
latmin = -48 
map = Basemap(llcrnrlon=lonmin,llcrnrlat=latmin,urcrnrlon=lonmax,urcrnrlat=latmax, resolution = 'i') 

map.drawmapboundary(fill_color='white') 
map.fillcontinents(color='white',lake_color='white') 
map.drawcoastlines() 

map.readshapefile('../data/raw/statsnzregional-council-2016-generalised-version-SHP/regional-council-2016-generalised-version', 'regional_council') 

ax = plt.gca() # get current axes instance 
cm = matplotlib.cm.get_cmap('viridis') 
norm = matplotlib.colors.Normalize(vmin=0.05, vmax=0.35) 

for info, shape in zip(map.regional_council_info, map.regional_council): 
    poly = Polygon(shape, facecolor=cm(norm(region_percent[info['REGC2016_N']])),edgecolor='k') 
    ax.add_patch(poly) 

plt.show() 

。このイメージは私が欲しいものに非常に近いですが、私は海の色を塗るのではなく、陸の境界線で止まるように色付けしたいと思います。

私はBasemapのmaskoceans()を調べましたが、これはおそらくこれを解決する最善の方法だと思いますが、自分の状況にそれを適用する方法がわかりません(例:緯度、

また、ニュージーランドの地図境界を硬い境界にする方法があります。そのため、ポリゴンパッチと内部の重なり部分だけが印刷されますか?

enter image description here

答えて

2

あなたは余分な領域をマスクするためにいくつかのポリゴンを必要とします。 ここでマスクファイル(nz_mask_w.shp、nz_mask_e.shp)を取得します: https://github.com/swatchai/cartopy_asean_proj/tree/master/shape_files をそして、これはコードです:

import matplotlib 
import matplotlib.pyplot as plt 
from mpl_toolkits.basemap import Basemap 
from matplotlib.patches import Polygon 
from matplotlib.collections import PatchCollection 
import shapefile # used to read my shapefiles 

fig = plt.figure(figsize=(15,15)) 
lonmax = 179.95 
lonmin = 165 
latmax = -33 
latmin = -48 
map = Basemap(llcrnrlon=lonmin, \ 
       llcrnrlat=latmin, \ 
       urcrnrlon=lonmax, \ 
       urcrnrlat=latmax, \ 
       resolution = 'i') 

# this is map theme (change to your need) 
map.readshapefile('data/statsnzregional/regional-council-2016-generalised-version', \ 
        name='regional_council') 

ax = plt.gca() # get current axes instance 

#cm = matplotlib.cm.get_cmap('viridis') 
#norm = matplotlib.colors.Normalize(vmin=0.05, vmax=0.35) 

for info, shape in zip(map.regional_council_info, map.regional_council): 
    poly = Polygon(shape, \ 
       facecolor=cm(norm(int(info['REGC2016']))/100.), \ 
       edgecolor='k', \ 
       zorder=1) 
    # original:- facecolor=cm(norm(info['REGC2016'])) 
    ax.add_patch(poly) 


# mask out the excess areas (use files in data sub folder) 
sf = shapefile.Reader("data/nz_mask_w") 
ss = sf.shapes() 
poly1 = Polygon(ss[0].points) 

sf = shapefile.Reader("data/nz_mask_e") 
ss = sf.shapes() 
poly2 = Polygon(ss[0].points) 

ax.add_collection(PatchCollection([poly1,poly2], \ 
            zorder=12, \ 
            facecolor='lightblue', \ 
            edgecolor='lightblue')) 

map.drawcoastlines(color='blue', linewidth=0.3) 
plt.show() 

enter image description here

関連する問題