将位置重新指定到栅格定义的纬度和经度

2024-06-16 11:22:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些英国监测点的臭氧水平数据,每个监测点的纬度和经度如下:

df = pd.DataFrame([[33.      , 52.411563, -1.560228],
       [45.      , 52.437165, -1.829999],
       [31.      , 52.47609 , -1.875024],
       [53.      , 50.5976  , -3.71651 ],
       [39.      , 50.37167 , -4.142361],
       [41.      , 50.725083, -3.532465],
       [69.      , 51.05625 , -2.68345 ],
       [38.      , 51.462839, -2.584482],
       [56.      , 50.73957 , -1.826744]],columns = ['values','lat','lon'])

我想将这些点绘制为网格正方形,使用官方网格-0.25x0.3125km,由位于同一网格框内的点的平均值着色,其纬度和经度如下所示:

grid = {'lon':np.linspace(-15.00,40,177),
            'lat':np.linspace(32.75,61.25,115)} #should define the grid squares for the map

我试着遍历纬度和经度列,将每个值重新分配到最近的纬度对。这似乎奏效了

for i, [time, val, lat, lon] in DEFRAO3.iterrows():
    pos_lat = bisect_left(nested_grid['lat'],lat)
    new_lat = nested_grid['lat'][pos_lat]
    pos_lon = bisect_left(nested_grid['lon'],lon)
    new_lon = nested_grid['lon'][pos_lat]

    DEFRAO3.set_value(i, 'latitude', new_lat)
    DEFRAO3.set_value(i, 'longitude', new_lon)
DEFRAO3

给出:

grid-adjusted measurement locations

然后,对具有相同(lat,lon)的位置进行平均:

newDEFRA = DEFRAO3.groupby(['latitude','longitude'], as_index=False).mean()

但是当我画它的时候,我所有的点都在同一条对角线上

All points are along the same diagonal line, which is not correct


Tags: thepos网格newfornpgridnested
1条回答
网友
1楼 · 发布于 2024-06-16 11:22:40

seaborn库包含一些用于此类绘图的有用绘图函数

以下代码将生成我认为您正在寻找的绘图:

import seaborn as sns
import pandas as pd

df = ...

# you might want to round lat/lon as appropriate before grouping

df2 = df.groupby(["lat", "lon"]).mean()
df2 = df2.unstack()  # Turns your long dataframe into a 2D dataframe.
df2 = df2.sort_index(ascending=False)  # Highest latitude first

sns.heatmap(df2, cmap="viridis", cbar="right")
plt.show()

相关问题 更多 >