简体   繁体   中英

Using cartopy to plot a binary map of Earth

I'm trying to use cartopy to plot a map of Earth with only 2 colors. The lands, filled in white and the ocean in dark for example. For now, I use the coastlines() method that creates the contour of the continents, but i don't know how to fill them with a colour, or to fill the ocean with one.

 import matplotlib.pyplot as plt

 fig = plt.figure(figsize=(12,6))
 ax = fig.add_subplot(1,1,1,projection = ccrs.EckertIV())
 ax.clear()
 cs = ax.contourf(longitude,lattitude,A,transform=ccrs.PlateCarree(),
    cmap='gist_gray',alpha=0.3)#,extend = 'both')
 ax.coastlines(color = 'black')

It produces a map with the continents, but it is all white, except the coastlines in black. Any thoughts on how to fill continents and/or oceans with a color? I've thought about using the stock_img() method but it does not uniformly fill the land with one colour.

Thank you in advance !

PS: this is my first post so please tell me if you need more precision on my problem or if I need to edit my post in a certain way

Here is a minimal example. In your example, you didn't show what is your longitude , latitude and A data, so I just replaced with a simple line.

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

latitude = [0, 10]
longitude = [0, 20]

fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.EckertIV())

ax.set_global()
# set_extent([-180, 180, -90, 90], crs=ccrs.PlateCarree())

ax.patch.set_facecolor(color='black')
# or
# ax.background_patch.set_facecolor(color='black')   # cartopy < v0.18
# or
# ax.add_feature(cartopy.feature.OCEAN, color='black')

ax.add_feature(cartopy.feature.LAND, color='white')

ax.plot(longitude, latitude, color='red', transform=ccrs.PlateCarree())

You are using an EckertIV projection, so you have to inform the axis that your longitude and latitude values are referenced to a PlateCarree projection. That's why you have to use the crs kw in set_extent (if not using set_global ) and the transform kw in plot .

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM