Cartopy figsize不会改变地图的实际大小

时间:2018-05-13 00:16:14

标签: python python-3.x python-2.7 matplotlib cartopy

使用cartoopy时如何更改图形宽度和高度。 figsize只改变图周围的白色空间,而不是图形本身。有任何想法吗。

import cartopy.crs as ccrs 
import cartopy.feature as cfeature
import matplotlib.pyplot as plt

def main():
    fig = plt.figure(figsize=(10,10))
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
    ax.set_extent([-20, 60, -40, 45], crs=ccrs.PlateCarree())

    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.OCEAN)
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(cfeature.BORDERS, linestyle=':')
    ax.add_feature(cfeature.LAKES, alpha=0.5)
    ax.add_feature(cfeature.RIVERS)

    plt.show()

if __name__ == '__main__':
    main()

找到此代码here enter image description here

1 个答案:

答案 0 :(得分:3)

地图的大小只能通过set_extent()方法的范围来定义。更改figsize只会影响地图轴周围的白框。

修改

如果您想要放大地图而不改变其范围,我只会看到一个选项:更改投影。这里有一些例子:

enter image description here enter image description here enter image description here

正如您所看到的,某些投影会增加高度,而其他投影会显得更宽。您无法完全控制地图的宽高比,但您仍可以尝试查看某些投影是否适合您。

您可以在https://scitools.org.uk/cartopy/docs/v0.15/crs/projections.html找到投影列表。

以下是生成第一张图片的代码:

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(3, 3))

ax = fig.add_subplot(1, 1, 1, projection=ccrs.LambertCylindrical())
ax.set_extent([-20, 60, -40, 45])
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.add_feature(cfeature.LAKES, alpha=0.5)
ax.add_feature(cfeature.RIVERS)
ax.set_title('LambertCylindrical')

plt.savefig('LambertCylindrical.png', dpi=100)