【问题标题】:converting map coordinates to lat/long将地图坐标转换为纬度/经度
【发布时间】:2014-01-08 23:51:04
【问题描述】:

已经收到了一个非常有用且有用的解决我的轮廓不规则数据的问题,Contours with map overlay on irregular grid in python,这里我仍然有将轮廓与底图结合的问题。为此,我知道我必须这样做: x,y = m(lon,lat)。 我看到的所有 SO 帖子都不符合我的需求。我的问题是我在哪里(从我的各种可用参数中)得到lon, lat 在上述公式中使用的x,y? 这是我的地图对象:

m = Basemap(projection = 'merc',llcrnrlon = 21, llcrnrlat = -18, urcrnrlon = 34, urcrnrlat = -8, resolution='h')

这是等高线和网格的数据:

data = pd.read_csv('meansr.txt', delim_whitespace=True)
numcols, numrows = 300, 300
xi = np.linspace(data.Lon.min(), data.Lon.max(), numcols)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numrows)
xi, yi = np.meshgrid(xi, yi)

x, y, z = data.Lon.values, data.Lat.values, data.Z.values
zi = griddata(x, y, z, xi, yi)

这里是绘图命令

plt.figure()
plt.contourf(xi, yi, zi)
plt.scatter(data.Lon, data.Lat, c=data.Z, s=100,
vmin=zi.min(), vmax=zi.max())
plt.colorbar()
plt.show()

这给了我两个并排的图,一个底图和等高线,我认为这是由于两个坐标的差异。指向等高线内表示正确的纬度和经度,而在地图上显示的x, y 非常大,大约为2000 及以上。 请帮忙。

【问题讨论】:

    标签: python matplotlib-basemap


    【解决方案1】:

    稍微更改了我的回复,以便我在回复您问题的更改版本时输入的内容:https://stackoverflow.com/a/20950887/2393569 使用相同的名称/...与您在此处提供的代码中一样:

    data = pd.read_csv('meansr.txt', delim_whitespace=True) 
    numcols, numrows = 30, 30 
    xi = np.linspace(data.Lon.min(), data.Lon.max(), numrows)
    yi = np.linspace(data.Lat.min(), data.Lat.max(), numcols)
    xi, yi = np.meshgrid(xi, yi)
    x, y, z = data.Lon, data.Lat, data.Z
    
    # NOTE: from here on it changes
    points = np.vstack((x,y)).T 
    values = z
    wanted = (xi, yi)
    zi = griddata(points, values, wanted) # for more info on griddata: http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html
    
    # from here its the same code again as you had
    plt.figure()
    plt.contourf(xi, yi, zi)
    plt.scatter(data.Lon, data.Lat, c=data.Z, s=100, vmin=zi.min(), vmax=zi.max())
    plt.colorbar()
    plt.show()
    

    所以你实际遇到的问题不是完全理解scipy.interpolate.griddata,也不是与matplotlib 相关的问题。

    所以正确的用法是

    scipy.interpolate.griddata(points, values, xi, method='linear', fill_value=nan)
    # 'points' has to contain what you named  x and y
    # 'values' has to contain what you named z
    # 'xi' has to contain what you named xi,yi
    

    更改此设置后,绘图是否仍然给您带来麻烦?

    注意:我假设你的进口是:

    import numpy as np
    from scipy.interpolate import griddata
    import pylab as plt
    # i guess the pd.read_csv is something pandas (heard of it, but never used it before, but since the only thing you do is read_csv i guess that wont cause problems)
    

    如果您在这里提出问题,最好也提供您的导入,因为我刚刚发现 from matplotlib.mlab import griddata 也存在,并且语法与您使用的类似,在 Contours with map overlay on irregular grid in python 中您使用来自的 griddata scipy,但是你似乎基于你的后续问题(这个)的答案包含 griddata 的 matplotlib.mlab 导入(它的工作方式不同)。这能解决你的困惑吗?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    相关资源
    最近更新 更多