Geopandas world map in Polar Stereographic projection with coloured oceans

旧时模样 提交于 2019-12-11 04:19:41

问题


Adding a further requirement to this question, I also need to have the oceans in blue (or any other colour).

For the 'PlateCarree' projection I can simply do this

crs = ccrs.PlateCarree()
crs_proj4 = crs.proj4_init
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
w = world.to_crs(crs_proj4)
g = w.plot(facecolor='sandybrown', edgecolor='black')

And now adding the ocean colour

g.set_facecolor('#A8C5DD')

If I now want to use a polar stereographic peojection

ccrs.NorthPolarStereo()

or

ccrs.SouthPolarStereo()

the projection does not work. When applying the answer to this question, I cannot get the oceans coloured


回答1:


You need to plot the map geometries on Cartopy geoaxes, and use cartopy.feature.OCEAN to plot the ocean. Here is the working code that you may try. Read the comments in the code for clarification.

import geopandas as gpd
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy

facecolor = 'sandybrown'
edgecolor = 'black'
ocean_color = '#A8C5DD'

#crs1 = ccrs.SouthPolarStereo()
crs1 = ccrs.NorthPolarStereo()

world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
w1 = world.to_crs(crs1.proj4_init)

fig1, ax1 = plt.subplots(figsize=(7,7), subplot_kw={'projection': crs1})

# useful code to set map extent,
# --- if you want maximum extent, comment out the next line of code ---
ax1.set_extent([-60.14, 130.4, -13.12, -24.59], crs=ccrs.PlateCarree())

# at maximum extent, the circular bound trims map features nicely
ax1.add_geometries(w1['geometry'], crs=crs1, \
                facecolor=facecolor, \
                edgecolor=edgecolor, \
                linewidth=0.5)

# this adds the ocean coloring
ax1.add_feature(cartopy.feature.OCEAN, facecolor=ocean_color, edgecolor='none')

plt.show()

The output plot will be:



来源:https://stackoverflow.com/questions/55651437/geopandas-world-map-in-polar-stereographic-projection-with-coloured-oceans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!