每公里每年绘制密度图

时间:2017-06-02 14:26:08

标签: python-2.7 contourf histogram2d

我有一个包含6年(日期,经度,纬度,数值)数据的csv文件我使用histogramm2d和每公里的contourf绘制了密度图,我得到了一张漂亮的地图,但我相信我绘制了每公里的密度每6年一次,所以我需要考虑一下这个标准,即知道我在文件中有多少年,并绘制每年每公里密度而不是每6年的密度。  所以这是我用来实现这个目的的代码:

with open('flash.csv') as f:
reader = csv.reader(f)
next(reader) # Ignore the header row.
lonMin, lonMax, dLon = -20.0, 5.0, 5
latMin, latMax, dLat = 18.0, 40.0, 5
for row in reader:
    lat = float(row[2])
    lon = float(row[3])
    # filter lat,lons to (approximate) map view:
    if lonMin <= lon <= lonMax and latMin <= lat <= latMax:
        lats.append( lat )
        lons.append( lon )

m = Basemap(llcrnrlon=min(lons), llcrnrlat=min(lats), urcrnrlon=max(lons), urcrnrlat=max(lats), projection='merc', resolution='f')

numcols = (max(lons)-min(lons)) * 100
numrows = (max(lats)-min(lats)) * 100

db = 1
lon_bins = np.linspace(min(lons)-db, max(lons)+db, numcols)
lat_bins = np.linspace(min(lats)-db, max(lats)+db, numrows) 
h, xedges, yedges = (np.histogram2d(lats, lons,[lat_bins, lon_bins]))
xi, yi= m(*np.meshgrid(lon_bins, lat_bins))

#shape into continuous matrice
g = np.zeros(xi.shape)
g[:-1,:-1] = h
g[-1] = g[0]      # copy the top row to the bottom
g[:,-1] = g[:,0]  # copy the left column to the right
print g.shape,yi.shape,xi.shape

m.drawcoastlines()
m.drawstates()

g[g==0.0] = np.nan
cs = m.contourf(xi, yi, g)
cbar = plt.colorbar(cs, orientation='horizontal')
cbar.set_label('la densite des impacts foudre',size=18)

plt.gcf().set_size_inches(15,15)
plt.show()

任何想法??

1 个答案:

答案 0 :(得分:0)

我刚刚找到了我的问题的解决方案,我这样做是为了获得我在csv文件中的年数,我将计算的密度除以NByears并且它运行良好。

DateMax = data.index.year.max()
DateMin = data.index.year.min()
NByears = (DateMax - DateMin)