The role of albinism
Python implementation
- Python uses the shp file to whiten the drawing, without the need to make a mask file, and can efficiently draw areas
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cf
import matplotlib.pyplot as plt
import cartopy.io.shapereader as shpreader
from cartopy.mpl.ticker import LatitudeFormatter,LongitudeFormatter
from matplotlib.path import Path
from cartopy.mpl.patch import geos_to_path
plt.rcParams['font.sans-serif']=['KaiTi']
shp_path=r'.\省.shp'
shp_data=shpreader.Reader(shp_path)
fig=plt.figure(figsize=(3,2),dpi=500)
ax1=plt.subplot(121,projection=ccrs.PlateCarree())
ax2=plt.subplot(122,projection=ccrs.PlateCarree())
for i,ax in enumerate([ax1,ax2]):
ax.add_geometries(shp_data.geometries(),crs=ccrs.PlateCarree(),edgecolor='k',facecolor='none',lw=0.5)
ax.set_extent([70, 140, 0, 55],crs=ccrs.PlateCarree())
ax.set_xticks(np.arange(70,140,5))
ax.set_yticks(np.arange(0,55,5))
ax.tick_params(direction='in',labelsize=3,top=True,right=True,length=2,width=0.5)
if i==0:
ax.set_title('未白化',fontsize=6)
else:
ax.set_title('白化后',fontsize=6)
x=np.arange(70, 140, 0.02)
y=np.arange(0, 55, 0.02)
X,Y=np.meshgrid(x,y)
Z=(X-108)**2+(Y-29)**2
for i,ax in enumerate([ax1,ax2]):
if i==0:
ax.contourf(X,Y,Z)
else:
ac=ax.contourf(X,Y,Z)
records=shp_data.records()
for record in records:
if record.attributes["省"] in ["河南省"]:
path=Path.make_compound_path(*geos_to_path([record.geometry]))
for collection in ac.collections:
collection.set_clip_path(path, transform=ax2.transData)
file_nineline = ".\九段线.shp"
reader_nineline = shpreader.Reader(file_nineline)
ax.add_geometries(reader_nineline.geometries(), crs=ccrs.PlateCarree(), lw=0.5, fc='none')
plt.show()