簡體   English   中英

cartopy shapefile 變成了一個點

[英]cartopy shapefile turning out as a dot

我正在嘗試從底圖切換到 cartopy,並且在繪制簡單的 shapefile 時遇到了一些困難。

使用底圖我可以得到這個

在此處輸入圖像描述

代碼:

from mpl_toolkits.basemap import Basemap
# define map extent
lllat=1.1497
urlat=1.5133
lllon=103.5822
urlon=104.1579

# Set up Basemap instance
m = Basemap(
    projection = 'merc',
    llcrnrlon = lllon, llcrnrlat = lllat, urcrnrlon = urlon, urcrnrlat = urlat,
    resolution='h')

shp_info = m.readshapefile('singapore_shapefile',
                            'singapore',
                            drawbounds=True)

但是對於cartopy,我得到一個中間有一個點的空白。 在此處輸入圖像描述

import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
from cartopy.feature import ShapelyFeature

# define map extent
lllat=1.1497
urlat=1.5133
lllon=103.5822
urlon=104.1579

# Set up cartopy instance
m = ccrs.Mercator()
ax = plt.axes(projection=m)
plt.gcf().set_size_inches(20, 10)

reader = shpreader.Reader("singapore_shapefile")
shape_feature = ShapelyFeature(reader.geometries(), m, facecolor="w", 
                               edgecolor='black', lw=1)
ax.add_feature(shape_feature)

plt.savefig("test.png")

任何人都可以指出我正確的方向嗎?

形狀文件: https://drive.google.com/file/d/1suB-VD65bmwesJo01mAvVUolRReWUkdV/view?usp=sharing

有兩點需要注意:

  1. 您需要設置 map 范圍,否則,感興趣的區域對於整個世界來說太小了。
  2. ShapelyFeature function 需要 shapefile 的投影,而不是 map。

下面是修改后的代碼。

import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
from cartopy.feature import ShapelyFeature
import matplotlib.pyplot as plt

# define map extent
lllat=1.1497
urlat=1.5133
lllon=103.5822
urlon=104.1579

# Set up cartopy instance
m = ccrs.Mercator()
ax = plt.axes(projection=m)

plt.gcf().set_size_inches(20, 10)


extent=[lllon,urlon,lllat,urlat]
ax.set_extent(extent)

reader = shpreader.Reader("singapore_shapefile")
shape_feature = ShapelyFeature(reader.geometries(), ccrs.PlateCarree(), facecolor="w",
                               edgecolor='black', lw=1)
ax.add_feature(shape_feature)

plt.savefig("singapore.png")

這是 output plot。

在此處輸入圖像描述

暫無
暫無

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

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