【问题标题】:How to plot points using Basemap如何使用底图绘制点
【发布时间】:2017-08-02 14:51:15
【问题描述】:

我有一个 numpy 数组 precip_subset,其形状为 (31, 60, 48)precip_subset 是通过合并 31 个数据集创建的;数据集中的第一个变量代表天,第二个代表经度,第三个代表纬度。数据集中每个位置的降水量都有一个唯一值;例如,print(precip_subset[1,0,32]) 会给我一个值1.05

我将展示我到目前为止所做的代码:

data_low = precip_subset[(precip_subset > 0) & (precip_subset < 3.14062)]
anomalies = []
for val in data_low:
    if val < 1: 
        anomalies.append(val)
        print(anomalies)

data_low_indices1 = np.where((precip_subset > 0) & (precip_subset < 1))
data_low_indices2 = np.array(np.where((precip_subset > 0) & (precip_subset < 1))).T
anomalies_ind = []
for ind in data_low_indices2:
    anomalies_ind.append(ind)
    print(np.asarray(anomalies_ind))

基本上,通过这段代码,我可以在原始数据集中获得降水值及其索引,precip_subset。从这里开始,我想用底图绘制这些数据——我想在地图上找到一个异常点。 precip_subset 是这样创建的:

data_path = r"C:\Users\matth\Downloads\TRMM_3B42RT\3B42RT_Daily.201001.7.nc4"
f = Dataset(data_path)

latbounds = [ -31 , -19 ]  
lonbounds = [ 131, 146 ] 
lats = f.variables['lat'][:] 
lons = f.variables['lon'][:]

# latitude lower and upper index
latli = np.argmin( np.abs( lats - latbounds[0] ) )
latui = np.argmin( np.abs( lats - latbounds[1] ) ) 

# longitude lower and upper index
lonli = np.argmin( np.abs( lons - lonbounds[0] ) )
lonui = np.argmin( np.abs( lons - lonbounds[1] ) )

precip_subset = f.variables['precipitation'][ : , lonli:lonui , latli:latui ]

这就是我到目前为止使用底图绘制数据所做的工作:

m = Basemap(llcrnrlon=131.,llcrnrlat=-31,urcrnrlon=146.,urcrnrlat=-19.)
m.drawcoastlines()
m.drawparallels(np.arange(-90., 120., 30.), labels=[1, 0, 0, 0])
m.drawmeridians(np.arange(-180., 181., 45.), labels=[0, 0, 0, 1])
m.imshow(anomalies_ind)
plt.show()

但是,我得到的图像不是我想要的(见下图)。

有谁知道我如何使用底图绘制anomalies_ind,并标出发现每个异常的点?

【问题讨论】:

  • 为什么没人回答这个问题
  • 您没有包含任何数据。 anomalies_ind 的小样本,几行就足够了。

标签: python matplotlib matplotlib-basemap


【解决方案1】:

imshow 只是将您提供的数据绘制为图像,覆盖其他任何内容。所以绝对不是你想要的。由于您没有给出anomalies_ind 形状的任何指示,我在这里举了一个例子。您需要做的就是将 lats 和 lons 映射到 x/y 像素坐标,然后绘制它们。 'o' 参数到 plot 只是说绘制圆圈,而不是线条。

anomalies_ind = [(-29.027008, 135.547227),
                 (-22.774524, 137.824754)]

lats = np.array(anomalies_ind)[:, 0]
lons = np.array(anomalies_ind)[:, 1]


m = Basemap(llcrnrlon=131.,llcrnrlat=-31,urcrnrlon=146.,urcrnrlat=-19.)
m.drawcoastlines()
m.drawparallels(np.arange(-90., 120., 30.), labels=[1, 0, 0, 0])
m.drawmeridians(np.arange(-180., 181., 45.), labels=[0, 0, 0, 1])
#m.imshow(anomalies_ind)

x, y = m(lons, lats)
m.plot(x, y, 'o')

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-06
    • 2020-07-19
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2017-04-16
    • 2015-07-12
    • 2020-09-14
    相关资源
    最近更新 更多