我有一个问题,我想在多边形内绘制一个热图,现在我可以绘制所有图,但是我不能强迫仅绘制多边形内的区域,有什么建议吗? 这是代码,谢谢大家
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import numpy.ma as ma
from numpy.random import uniform, seed
# make up some randomly distributed data
seed(1234)
npts = 200
x=df_heat2.lon.values
y=df_heat2.lat.values
z=df_heat2.Val.values
xi=np.linspace(min(df_heat2.lon.values)-0.1,max(df_heat2.lon.values)+0.1,100)
yi=np.linspace(min(df_heat2.lat.values)-0.1,max(df_heat2.lat.values)+0.1,100)
fig, ax = plt.subplots(figsize=(20, 15))
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='nearest')
# contour the gridded data, plotting dots at the randomly spaced data points.
#CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k') # B/W
CS = plt.contourf(xi,yi,zi,15,cmap='BuPu')
plt.colorbar() # draw colorbar
# plot data points.
plt.scatter(x,y,marker='o',c='green',s=5)
plt.title('griddata test (%d points)' % npts)
shapefile2.geometry.boundary.plot(color='red',edgecolor='k',linewidth = 2,ax=ax)
plt.show()