繁体   English   中英

如何使用 matplotlib 底图在底图上 plot 数据

[英]How to plot data on a basemap using matplotlib basemap

我的代码的两个部分给我带来了麻烦,我正在尝试在此处获取第一部分中创建的底图:

#Basemap
epsg = 6060; width = 2000.e3; height = 2000.e3 #epsg 3413. 6062
m=Basemap(epsg=epsg,resolution='l',width=width,height=height) #lat_ts=(90.+35.)/2.
m.drawcoastlines(color='white')
m.drawmapboundary(fill_color='#99ffff')
m.fillcontinents(color='#cc9966',lake_color='#99ffff')
m.drawparallels(np.arange(10,70,20),labels=[1,1,0,0])
m.drawmeridians(np.arange(-100,0,20),labels=[0,0,0,1])
plt.title('ICESAT2 Tracks in Greenland')
plt.figure(figsize=(20,10))

然后我的下一部分是指 plot 从文件中获取的数据,以及 plot 这些轨道位于底图之上。 相反,它完全创建了一个新的 plot。 我曾尝试改写辅助 plt.scatter 以匹配 Basemap,例如 m.scatter、m.plt 等。但是当我这样做时,它只会返回“RuntimeError:不能将单个艺术家放入多个图中”。 关于如何将下一部分代码放到底图上的任何想法? 这里是下一节,重点看结尾,看看它在哪里绘制。

icesat2_data[track] = dict() # creates a sub-dictionary, track
    icesat2_data[track][year+month+day] = dict() # and one layer more for the date under the whole icesat2_data dictionary
    icesat2_data[track][year+month+day] = dict.fromkeys(lasers)
    for laser in lasers: # for loop, access all the gt1l, 2l, 3l
        if laser in f:    
            lat = f[laser]["land_ice_segments"]["latitude"][:] # data for a particular laser's latitude.
            lon = f[laser]["land_ice_segments"]["longitude"][:] #data for a lasers longitude
            height = f[laser]["land_ice_segments"]["h_li"][:] # data for a lasers height
            quality = f[laser]["land_ice_segments"]["atl06_quality_summary"][:].astype('int')
        
                # Quality filter
            idx1 = quality == 0 # data dictionary to see what quality summary is
            #print('idx1', idx1)
        
                # Spatial filter
            idx2 = np.logical_and( np.logical_and(lat>=lat_min, lat<=lat_max), np.logical_and(lon>=lon_min, lon<=lon_max) )
        
            idx = np.where( np.logical_and(idx1, idx2) ) # combines index 1 and 2 from data quality filter. make sure not empty. if empty all data failed test (low quality or outside box)

            icesat2_data[track][year+month+day][laser] = dict.fromkeys(['lat','lon','height']) #store data, creates empty dictionary of lists lat, lon, hi, those strings are the keys to the dict.
            icesat2_data[track][year+month+day][laser]['lat'] = lat[idx] # grabbing only latitudes using that index of points with good data quality and within bounding box
            icesat2_data[track][year+month+day][laser]['lon'] = lon[idx] 
            icesat2_data[track][year+month+day][laser]['height'] = height[idx]
         
            if lat[idx].any() == True and lon[idx].any() == True:
                x, y = transformer.transform(icesat2_data[track][year+month+day][laser]['lon'], \
                                        icesat2_data[track][year+month+day][laser]['lat']) 

                plt.scatter(x, y, marker='o', color='#000000')

目前,它们分别为 output,如下所示:

在此处输入图像描述

在此处输入图像描述

我想你可能需要:

plt.figure(figsize(20,10))

在创建底图之前,而不是之后。 就目前而言,它正在创建一个 map ,然后在此之后创建一个新数字,这就是为什么你会得到两个数字。

然后你的绘图线应该是 m.scatter() 正如你之前提到的那样。

不确定您是否仍在为此工作,但这是我整理的一个简单示例,您可能可以使用(显然我没有您正在使用的数据)。 有几件事可能无法自我解释——我使用 m() 将坐标转换为 map 坐标。 这是 Basemap 的内置转换方法,因此您不必使用 PyProj。 此外,在散点图 function 中设置 zorder 可确保您的点绘制在国家图层上方,并且不会隐藏在下方。

#Basemap

epsg = 6060; width = 2000.e3; height = 2000.e3 #epsg 3413. 6062
plt.figure(figsize=(20,10))
m=Basemap(epsg=epsg,resolution='l',width=width,height=height) #lat_ts=(90.+35.)/2.
m.drawcoastlines(color='white')
m.drawmapboundary(fill_color='#99ffff')
m.fillcontinents(color='#cc9966',lake_color='#99ffff')
m.drawparallels(np.arange(10,70,20),labels=[1,1,0,0])
m.drawmeridians(np.arange(-100,0,20),labels=[0,0,0,1])
plt.title('ICESAT2 Tracks in Greenland')


for coord in [[68,-39],[70,-39]]:
    lat = coord[0]
    lon = coord[1]
    x, y = m(lon,lat)
    m.scatter(x,y,color='red',s=100,zorder=10)
              
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM