cnmaps
cnmaps copied to clipboard
避免裁剪出界问题
对于裁剪后裁剪内容超出 Axes
的情况(#97、#99、#111)
添加 contours.set_clip_box(ax.bbox)
即可解决。以文档中的 剪切地图 为例
测试代码为
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cnmaps import get_adm_maps, clip_contours_by_map, draw_map
from cnmaps.sample import load_dem
lons, lats, data = load_dem()
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
ax.set_extent([70, 140, 40, 55], crs=ccrs.PlateCarree())
map_polygon = get_adm_maps(country='中华人民共和国', record='first', only_polygon=True)
cs = ax.contourf(lons, lats, data,
cmap=plt.cm.terrain,
levels=np.linspace(-2800, data.max(), 10),
transform=ccrs.PlateCarree())
clip_contours_by_map(cs, map_polygon)
draw_map(map_polygon, color='k', linewidth=1)
plt.show()