如何根据数据而不是lat,lon坐标制作热图? R,Matplotlib

时间:2017-06-07 02:06:56

标签: matplotlib heatmap

我有12,000个数据点,其结构如下:(lon,lat,value)。我想根据值制作这些数据的热图,但我不希望地图受x,y密度的影响。由于地理限制,在某些地区只有更多的测量值。我已经使用加权点尝试了Google Maps API,但它们的实现仍然是点密度的因素。 我想使用Python或R来执行此操作,因为我想生成矢量格式图像。目前,我只有一个我的数据的散点图,其中点在该位置按值着色。 https://i.stack.imgur.com/XAWG5.jpg

理想情况下,这个数字看起来像这样:

https://i.stack.imgur.com/gppVC.png

这是我的代码:

#Create a map centered on Canada
maps = Basemap(projection = "merc", resolution = "l",area_thresh = 100,
               llcrnrlon = -143, llcrnrlat = 42, urcrnrlon = -50, urcrnrlat = 79)
maps.drawmapboundary(fill_color = "aqua", zorder = 1)
maps.fillcontinents(color='white',lake_color='aqua', zorder = 3)
maps.drawcountries(zorder = 5)
maps.drawstates(zorder = 7)

data = pd.read_csv("stats_by_coordinates.csv")
import matplotlib.cm as cm

vio_norm = np.log(data['vio']) # log normalize data
maxima = max(vio_norm) 
**# Scale data between ¨0 and 1 for color mapping**
vio_norm = [x/float(maxima) for x in vio_norm] 


x = []
y = []

for i in range(len(data)):
    lon,lat = maps(data['lon'][i], data['lat'][i]) #Convert to map coordinates
    x.append(lon)
    y.append(lat)

maps.scatter(x,y,zorder = 10, marker = "o",facecolor = vio_norm, edgecolor = "",
            alpha = 1.0,c = vio_norm,s = 1,lw = 0, cmap = py.cm.YlOrRd)
py.colorbar()
py.savefig('myimage.png', format='png', dpi=1200, bbox_inches = "tight") 

0 个答案:

没有答案