问题
I have an array anomalies_ind
that was created in this way:
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))
The output of this is this:
[[1, 23, 45]
[3, 45, 56]
...
[31, 45, 89]]
The first element represents the day in the month of January, while the 2nd and 3rd elements represent longitude and latitude respectively. I am trying to plot points at the longitudes and latitudes given on map like so:
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()
However, no points are on the map. Does anyone know what is wrong?
EDIT: Here are some values of the real foo
array"
[[ 0 0 0]
[ 0 0 16]
[ 0 0 17]
...,
[ 30 219 113]
[ 30 219 114]
[ 30 219 116]]
回答1:
I've run into this problem, myself.
Basemap plotting functions have a latlon
keyword, and changing this worked for me. The default is latlon=False
, so that x and y values are interpreted as projection coordinates. Adding latlon=True
tells Basemap to interpret x and y values as map coordinates.
See the Basemap documentation on scatter here: http://matplotlib.org/basemap/api/basemap_api.html#mpl_toolkits.basemap.Basemap.scatter
In your original plotting syntax, you'll want to change the line:
m.scatter(longs, lat, marker = 'o', color = 'k', zorder=10)
to:
m.scatter(longs, lat, marker = 'o', color = 'k', zorder=10, latlon=True)
回答2:
You have to convert your latitudes ans longitudes to map projection before calling scatter:
x,y=m(longs,lat)
m.scatter(x, y, marker = 'o', color = 'k', zorder=10)
来源:https://stackoverflow.com/questions/45511792/scatter-does-not-plot-any-points-on-basemap