簡體   English   中英

使用matplotlib-basemap將一個多邊形添加到繪圖中

[英]Adding one polygon to a plot using matplotlib-basemap

我有以下代碼:

from mpl_toolkits.basemap import Basemap
map = Basemap(projection='merc', lat_0=50, lon_0=4,
    resolution = 'l', area_thresh = 0.1,
    llcrnrlon=4, llcrnrlat=50,
    urcrnrlon=40, urcrnrlat=60)

map.drawcoastlines(linewidth=0.15)
map.drawcountries(linewidth=0.15)
map.fillcontinents(color='brown',lake_color='white')
map.drawmapboundary(fill_color='white')

在此輸入圖像描述

在這張地圖的頂部,我想顯示一個只包含一個多邊形的shapefile。 多邊形定義了一個封閉區域。 我已經找到了關於如何手動添加多邊形或從shapefile中繪制多個多邊形的不同教程,但我無法為我的情況做這些。 shapefile屬性表僅由兩個字段組成:' c '和' area '。

現在我已經到了這里

import shapefile

s = shapefile.Reader(filepath,'c',drawbounds=False)
shapes = s.shapes()
records = s.records()
for record, shape in zip(records,shapes):
    lons,lats = zip(*shape.points)
    data = np.array(map(lons, lats)).T
x, y =map(lons,lats) 

有同樣的問題,但它很簡單,你從來​​沒有想過用網上的許多教程和模塊以及類似的問題來做這件事:

map.readshapefile('luthuania', 'any_name_you_like', drawbounds=True)

所以對你的例子:

from mpl_toolkits.basemap import Basemap
map = Basemap(projection='merc', lat_0=50, lon_0=4,
    resolution = 'l', area_thresh = 0.1,
    llcrnrlon=4, llcrnrlat=50,
    urcrnrlon=40, urcrnrlat=60)

map.readshapefile('luthuania', 'any_name_you_like', drawbounds=True, linewidth=2, color='b')

map.drawcoastlines(linewidth=0.15)
map.drawcountries(linewidth=0.15)
map.fillcontinents(color='brown',lake_color='white')
map.drawmapboundary(fill_color='white')

這使

立陶宛

順便說一句,模塊shapefile由底層地圖使用:參見C:\\ Python33 \\ Lib \\ site-packages \\ mpl_toolkits \\ basemap \\ shapefile.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM