Python Matplotlib:在特定点上绘制Basemap上的数据

时间:2022-09-10 22:02:23

I am plotting weather model precipitation data on a map. The map is a contour fill. Over that data I would like to plot text at specific grid points that tells the value of the precipitation at the point in time, however I am struggling to do this. I have a list of lat/lon points but cannot figure out how to map it properly to the data I have. The data itself comes with it's own set of lat/lon points that uniquely map to the data. Here's example code that I have:

我正在绘制地图上的天气模型降水数据。地图是轮廓填充。在那些数据上,我想在特定的网格点绘制文本,告诉时间点的降水值,但是我很难做到这一点。我有一个lat / lon点列表,但无法弄清楚如何将它正确映射到我拥有的数据。数据本身带有它自己的一组lat / lon点,它们唯一地映射到数据。这是我的示例代码:

grib = 'gfs.t00z.pgrb2.0p25.f084'
grbs = pygrib.open(grib)

grb = grbs.select(name='Total Precipitation',typeOfLevel='surface')[0]
precip = grb.values *.039370
lat,lon = grb.latlons()
x, y = m(lon,lat)

with open('mydata') as f:
    for line in f:
        myline = line.replace("\n", "")
        myline = myline.split(",")
        uniquepoints.append(myline) # contains specific lat, lon points

intervals = [0.0,0.01,0.1,0.25,0.5,0.75,1.00,1.25,1.50,1.75,2.0,2.50,3.00]

m=Basemap(projection='lcc',lon_0 = -95.,llcrnrlon=-125.,
  urcrnrlon=-55.,llcrnrlat=20.,urcrnrlat=50., lat_1=25.,lat_2=46., resolution='l',area_thresh=10000,ax=ax)

obsobj = m.contourf(x,y,precip,intervals,cmap=plt.cm.jet)

I know you are suppose to use plt.text, but I can't configure it correctly so that the unique points map correctly to the precip data.

我知道您假设使用plt.text,但我无法正确配置它,以便唯一的点正确映射到沉降数据。

1 个解决方案

#1


It can be easy to make simple mistakes with placing points on Basemap as you have to convert between lat,long,height and the windows coordinate system in x,y. However, if your data is plotting at the correct points on the map, you can use those positions to plot your labels at those exact points with some specified offset.

在底图上放置点可以很容易地做出简单的错误,因为你必须在纬度,长度,高度和x,y中的窗口坐标系之间进行转换。但是,如果您的数据绘制在地图上的正确点上,则可以使用这些位置在具有指定偏移量的精确点处绘制标签。

A pythonic example of how to do this:

如何执行此操作的pythonic示例:

lats = [x['LLHPosition'][0] for x in unit_data]
lons = [x['LLHPosition'][1] for x in unit_data]
x,y = m(lons,lats)

label_text = [x['UnitName'] for x in unit_data]
x_offsets = [2000] * len(unit_data)
y_offsets = [2000] * len(unit_data)

for label, xpt, ypt, x_offset, y_offset in zip(label_text, x, y, x_offsets, y_offsets):
    plt.text(xpt+x_offset, ypt+y_offset, label)

#1


It can be easy to make simple mistakes with placing points on Basemap as you have to convert between lat,long,height and the windows coordinate system in x,y. However, if your data is plotting at the correct points on the map, you can use those positions to plot your labels at those exact points with some specified offset.

在底图上放置点可以很容易地做出简单的错误,因为你必须在纬度,长度,高度和x,y中的窗口坐标系之间进行转换。但是,如果您的数据绘制在地图上的正确点上,则可以使用这些位置在具有指定偏移量的精确点处绘制标签。

A pythonic example of how to do this:

如何执行此操作的pythonic示例:

lats = [x['LLHPosition'][0] for x in unit_data]
lons = [x['LLHPosition'][1] for x in unit_data]
x,y = m(lons,lats)

label_text = [x['UnitName'] for x in unit_data]
x_offsets = [2000] * len(unit_data)
y_offsets = [2000] * len(unit_data)

for label, xpt, ypt, x_offset, y_offset in zip(label_text, x, y, x_offsets, y_offsets):
    plt.text(xpt+x_offset, ypt+y_offset, label)