【问题标题】:Reassign positions to the latitudes and longitudes defined by a grid将位置重新分配给网格定义的纬度和经度
【发布时间】:2019-10-24 13:40:33
【问题描述】:

我有一些英国监测站点的臭氧水平数据,并给出了每个站点的纬度和经度,例如:

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

给予:

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

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

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

【问题讨论】:

    标签: python pandas plot


    【解决方案1】:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      相关资源
      最近更新 更多