我的问题和这个问题相同:Fill oceans in basemap。我知道这里给出了一个答案,但是当我尝试将答案应用于我的代码时,它是行不通的。
这是我的原始图形。
我想让海洋变白,只显示南极洲的颜色差异。
这是我尝试过的代码:
from mpl_toolkits.basemap import Basemap
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Path, PathPatch
D = pd.read_pickle('directory')
fig, ax = plt.subplots()
lons = D['longitude'][:]
lats = D['latitude'][:]
lon, lat = np.meshgrid(lons, lats)
variable_to_graph = D['variable_to_graph']
m = Basemap(projection='spstere', boundinglat=-60, lon_0=180, resolution='l')
m.drawcoastlines(color='k') # shows Antarctica
x,y = m(lon, lat)
ax.pcolormesh(x, y, variable_to_graph, cmap='viridis', vmin=-400, vmax=-0)
# Getting the limits of the map:
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
map_edges = np.array([[x0,y0], [x1,y0], [x1,y1], [x0,y1]])
# Getting all polygons used to draw the coastlines of the map
polys = [p.boundary for p in m.landpolygons]
# Combining with map edges.
polys = [map_edges]+polys[:]
# Creating a PathPatch.
codes = [
[Path.MOVETO] + [Path.LINETO for p in p[1:]]
for p in polys
]
polys_lin = [v for p in polys for v in p]
codes_lin = [c for cs in codes for c in cs]
path = Path(polys_lin, codes_lin)
patch = PathPatch(path,facecolor='white', lw=0)
# Masking the data:
ax.add_patch(patch)
plt.show()
哪个给我这个
我也尝试过使用is_land()函数,但是由于分辨率相对较低(1x1度),因此图像不是很整齐。
对于如何使颜色图仅在南极洲显示以及如何使其停在南极洲的边缘,将不胜感激。
编辑:我已经用可以测试的代码更新了我的问题。
from mpl_toolkits.basemap import Basemap
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Path, PathPatch
# D = pd.read_pickle('directory')
fig, ax = plt.subplots()
lons = range(0,361)
lats = [-60, -61, -62, -63, -64, -65, -66, -67, -68, -69, -70,
-71, -72, -73, -74, -75, -76, -77, -78, -79, -80, -81,
-82, -83, -84, -85, -86, -87, -88, -89, -90]
lon, lat = np.meshgrid(lons, lats)
variable_to_graph = np.random.rand(31,361)
m = Basemap(projection='spstere', boundinglat=-60, lon_0=180, resolution='l')
m.drawcoastlines(color='k') # shows Antarctica
x,y = m(lon, lat)
ax.pcolormesh(x, y, variable_to_graph, cmap='viridis', vmin=-400, vmax=-0)
# Getting the limits of the map:
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
map_edges = np.array([[x0,y0], [x1,y0], [x1,y1], [x0,y1]])
# Getting all polygons used to draw the coastlines of the map
polys = [p.boundary for p in m.landpolygons]
# Combining with map edges.
polys = [map_edges]+polys[:]
# Creating a PathPatch.
codes = [
[Path.MOVETO] + [Path.LINETO for p in p[1:]]
for p in polys
]
polys_lin = [v for p in polys for v in p]
codes_lin = [c for cs in codes for c in cs]
path = Path(polys_lin, codes_lin)
patch = PathPatch(path,facecolor='white', lw=0)
# Masking the data:
ax.add_patch(patch)
plt.show()