python - Display heat map on a Matplotlib/Basemap

旧巷老猫 提交于 2019-12-13 03:46:34

问题


I have a dataset of longitudes/latitudes as follows:

id,spp,lon,lat
1a,sp1,1,9
1b,sp1,3,11
1c,sp1,6,12
2a,sp2,1,9
2b,sp2,1,10
2c,sp2,3,10
2d,sp2,4,11
2e,sp2,5,12
2f,sp2,6,12
3a,sp3,4,13
3b,sp3,5,11
3c,sp3,8,8
4a,sp4,4,12
4b,sp4,6,11
4c,sp4,7,8
5a,sp5,8,8
5b,sp5,7,6
5c,sp5,8,2
6a,sp6,8,8
6b,sp6,7,5
6c,sp6,8,3

I want to display a heat map representing the density of points over a geographical map using Matplotlib/Basemap, using the code below (more or less based on this one):

from mpl_toolkits.basemap import Basemap
from matplotlib import cm as cmap
import matplotlib.pyplot as plt
import numpy as np
import warnings

warnings.filterwarnings("ignore")

# input data
coords = np.loadtxt("testdata.csv", delimiter=',', skiprows=1, usecols=(2, 3))
lon = coords[:,0]
lat = coords[:,1]

# create map
m = Basemap(width=12000000, height=9000000, projection='lcc',
            resolution='c', lat_0=6, lon_0=4.)
m.drawcoastlines(linewidth=0.25)

# create heatmap
heatmap, xedges, yedges = np.histogram2d(lon, lat, bins=20)

# plot map
##plt.clf()
m.imshow(heatmap.T, interpolation='none', cmap=cmap.jet)
plt.show()

However, the heat map is displayed but the map is not shown (see attached figure)

This looks the opposite of this question, in which the map is displayed but not the heat map (however that solution was not very helpful to me).

Any hints?

来源:https://stackoverflow.com/questions/46555175/python-display-heat-map-on-a-matplotlib-basemap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!