Scatter不会在Basemap上绘制任何点

时间:2017-08-04 16:42:03

标签: python matplotlib matplotlib-basemap scatter

我有一个以这种方式创建的数组anomalies_ind

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

latbounds = [ -45 , -10 ]
lonbounds = [ 105, 160 ] 
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 ]

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

这个的输出是:

[[1, 23, 45]
 [3, 45, 56]
 ...
 [31, 45, 89]]

第一个元素代表一月份的一天,而第二个和第三个元素分别代表经度和纬度。我试图在地图上给出的经度和纬度上的点数如下:

foo = np.asarray(anomalies_ind)
longs = foo[:,1]
lat = foo[:,2]
m = Basemap(llcrnrlon=105.,llcrnrlat=-45,urcrnrlon=160,urcrnrlat=-10)
m.drawcoastlines()
m.fillcontinents(color = 'lightgray', zorder = 0)
m.scatter(longs, lat, marker = 'o', color = 'k', zorder=10)
plt.show()

然而,地图上没有任何积分。有谁知道出了什么问题?

编辑:以下是真实foo数组&#34;

的一些值
[[  0   0   0]
 [  0   0  16]
 [  0   0  17]
 ..., 
 [ 30 219 113]
 [ 30 219 114]
 [ 30 219 116]]

2 个答案:

答案 0 :(得分:1)

我自己也遇到过这个问题。

底图绘制功能有一个latlon关键字,更改此功能对我有用。默认值为latlon=False,因此x和y值被解释为投影坐标。添加latlon=True告诉Basemap将x和y值解释为地图坐标。

请在此处查看有关散点图的底图文档: http://matplotlib.org/basemap/api/basemap_api.html#mpl_toolkits.basemap.Basemap.scatter

在原始绘图语法中,您想要更改该行:

m.scatter(longs, lat, marker = 'o', color = 'k', zorder=10)

为:

m.scatter(longs, lat, marker = 'o', color = 'k', zorder=10, latlon=True)

答案 1 :(得分:0)

在调用散射之前,您必须将纬度和经度转换为地图投影:

x,y=m(longs,lat)
m.scatter(x, y, marker = 'o', color = 'k', zorder=10)