使用shapefile绘制热图
代码在这里:
map = Basemap(llcrnrlon=xc1,llcrnrlat=yc1,urcrnrlon=xc2,urcrnrlat=yc2)
##Assuming "shape.shp" is my shapefile
map.readshapefile('./shape','shape',zorder =1,)
patches=[]
cs=plt.cm.Greens(np.arange(18)/18.)
for info, shape in zip(map.shape_info, map.shape):
x,y=zip(*shape)
patches.append( Polygon(np.array(shape), True) ) # facecolor= '#6582B3'
ax.add_collection(PatchCollection(patches, facecolor= cs,edgecolor='none',
linewidths=1.5, zorder=2))
## scatter the point, assuming "pt" is the Dataframe
pt_lat = pt.lat.as_matrix()
pt_lon = power.lon.as_matrix()
plt.scatter(pt_lon,pt_lat,marker='o',s=50,lw= 0,zorder = 3, alpha = 0.75)
图片:
http://i11.tietuku.com/9785abb6097b7c0e.png
在图片中,每个shapefile的颜色基于预定义的色彩图。
- 绘制每个区域(在我的情况下,18 shapefile),其颜色对应于pt.values的总和。
- 换句话说,内点数据决定shapefile的颜色
感谢@ MaxNoe的回答。
我从你的代码中学到了什么,但仍有一些问题 这是我的代码&图片:
fig = plt.figure(figsize =(8,6))
ax = plt.subplot()
map = Basemap(llcrnrlon=xc1,llcrnrlat=yc1,urcrnrlon=xc2,urcrnrlat=yc2)
map.readshapefile('./shape','shape')
patches=[]
for info, shape in zip(map.shape_info, map.shape):
x,y=zip(*shape)
patches.append(Polygon(np.array(shape), True) )
xx = pt.lon.iloc[:].as_matrix()
yy = pt.lat.iloc[:].as_matrix()
value = pt.value.iloc[:].as_matrix()
sh = (len(xx),2)
position = np.zeros(len(xx)*2).reshape(*sh)
for i in range(0,len(xx),1):
position[i] = np.array([xx[i],yy[i]])
poly_values = []
for patch in patches:
mask = np.array([patch.contains_point(xy) for xy in position])
poly_values.append(value[mask].sum())
coll = PatchCollection(patches, cmap = 'Greens')
coll.set_array(np.array(poly_values))
ax.add_collection(coll)
plt.colorbar(coll,label = "polygon")
point_plot = plt.scatter(xx,yy,marker='o',s=80,lw= 0,zorder = 3, c = "r",alpha = 0.75)
ax.set_frame_on(False)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="4%", pad=0.1)
cbar = plt.colorbar(coll,label = "polygon",cax= cax)
http://i4.tietuku.com/9a7b0cbc16f2e0b0.png
- 看起来polygon [i]的颜色不符合poly_value [i]
- 我认为问题是
coll.set_array
不起作用。- 否则,我检查了每个多边形和其中的散点值,poly_value [i]和实际条件不匹配(大于现实)。我想我可能会使用value.mask错误。
答案 0 :(得分:1)
您可以使用Polygon.contains_point
检查一个点是否在其中。
我使用此函数创建一个布尔蒙版来处理多边形内的点,并使用.sum()
来获取此多边形的值。
然后我使用PatchCollection.set_array
来设置值。
这是代码(没有底图,因为我没有形状文件):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
# some random numbers for demonstration
data = np.random.normal(0, 1, (100, 2))
value = np.random.normal(0, 1, 100)
polygons = [
Polygon([(0, 0), (0, 3), (-3, 3), (-3, 0)], closed=True),
Polygon([(0, 0), (0, -3), (-3, -3), (-3, 0)], closed=True),
Polygon([(0, 0), (0, 3), (3, 3), (3, 0)], closed=True),
Polygon([(0, 0), (0, -3), (3, -3), (3, 0)], closed=True),
]
poly_values = []
for poly in polygons:
mask = np.array([poly.contains_point(xy) for xy in data])
poly_values.append(value[mask].sum())
coll = PatchCollection(polygons, cmap='magma')
coll.set_array(np.array(poly_values))
fig, ax = plt.subplots()
ax.add_collection(coll)
points = ax.scatter(data[:, 0], data[:, 1], c=value, cmap='viridis', linewidth=0)
fig.colorbar(coll, label='polygons')
fig.colorbar(points, label='points')
plt.show()