2017-06-02 54 views
-1

我有一個包含6年(日期,經度,緯度,數值)數據的csv文件,我繪製了使用histogramm2d和contourf每公里的密度圖,我有一張漂亮的地圖,但我相信我繪製了每6年每km的密度,所以我需要考慮知道我在文件中有多少年的標準,並繪製每年每km而不是每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() 

任何想法?

+0

您應該提供有關數據外觀的更多詳細信息,例如,如何表示'時間'。此外,您的方法似乎有點複雜:您是否考慮過使用熊貓來讀取數據? – Daan

+0

時間戳,heure,lat,lon,影響,類型 2007-01-01 00:00,13,138:433.837,-9.205,10.3,1 2007-01-02 00:00:00, 00:07:28,34.5293,-10.2384,17.7,1 2007-01-02 00:00:00,23:01:03,35.0617,-1.435,-17.1,2 2007-01-03 00:00 :00,01:14:29,36.5685,0.9043,36.8,1 2007-01-03 00:00:00,05:03:51,34.1919,-12.5061,-48.9,1 –

回答

0

我剛剛找到了我的問題的解決方案,我這樣做是爲了獲得我在csv文件中的年數,我將計算出來的密度分成了NByears,它的工作完美。

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