【问题标题】:How to plot data on a basemap using matplotlib basemap如何使用 matplotlib 底图在底图上绘制数据
【发布时间】:2020-11-06 13:49:06
【问题描述】:

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

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

然后我的下一部分旨在绘制从文件中获取的数据,并将这些轨迹绘制在 Basemap 的顶部。相反,它完全创建了一个新的情节。我曾尝试改写辅助 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')

目前,它们分别输出,如下所示:

【问题讨论】:

  • 语句plt.scatter() 创建了新的情节。您必须使用m.scatter() 才能继续在同一张地图上绘图。

标签: python python-3.x matplotlib scatter-plot matplotlib-basemap


【解决方案1】:

不确定您是否仍在研究此问题,但这是我整理的一个简单示例,您可能可以使用(显然我没有您正在使用的数据)。有几件事可能不是不言自明的——我使用 m() 将坐标转换为地图坐标。这是 Basemap 的内置转换方法,因此您不必使用 PyProj。此外,在 scatter 函数中设置 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()

【讨论】:

    【解决方案2】:

    我想你可能需要:

    plt.figure(figsize(20,10))
    

    在创建底图之前,而不是之后。就目前而言,它正在创建一个地图,然后创建一个新图形,这就是你得到两个图形的原因。

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

    【讨论】:

    • 谢谢!这并没有完全解决问题。但是现在 figsize 线使底图变大了。但第二个不再出现。所以至少这就是为什么它去一个新的图表。现在解决为什么其他代码没有出现在此处的同一位置
    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2018-03-05
    • 2015-03-21
    • 2014-10-11
    • 1970-01-01
    相关资源
    最近更新 更多