下面是我的代码,试图在其他一些数据的底图投影上绘制散点。我在代码中有数据的实际路径。
散点不会显示在底图投影的顶部。
from mpl_toolkits.basemap import Basemap, shiftgrid, cm
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import matplotlib as mpl
samosdata = Dataset()
d = Dataset()
sst = d.variables["analysed_sst"][:][0, ::-1, :]
lon = d.variables["lon"][:]
lat = d.variables["lat"][::-1]
lats = samosdata.variables['lat']
lons = samosdata.variables['lon']
time = samosdata.variables['time']
ts = samosdata.variables['TS']
ts = np.array(ts)
lons = np.array(lons)
lats = np.array(lats)
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
m = Basemap(projection='mill', llcrnrlat=-80, urcrnrlat=80,
llcrnrlon=-180, urcrnrlon=180, lat_ts=20, resolution='c')
nx = int((m.xmax - m.xmin)/11113.2); ny = int((m.ymax m.ymin)/11113.2)
sst = m.transform_scalar(sst, lon, lat, nx, ny)
im = m.imshow(sst, interpolation = "none")
x, y =m(list(lons), list(lats))
pts = plt.scatter(x,y, marker='o', s=5, c=ts, cmap='plasma')
plt.colorbar(pts)
m.drawcoastlines()
parallels = np.arange(-90, 90, 30)
meridians = np.arange(-180, 180, 60)
m.drawparallels(parallels, labels = [1, 0, 0, 1])
m.drawmeridians(meridians, labels = [1, 0, 0, 1])
cb = m.colorbar(im, "right", size = "5%", pad = "2%")
ax.set_title("SST 2010 01 01")
plt.show()
当我输入边界时,它将绘制散点,但不会准确显示背景数据。
m = Basemap(projection='mill', llcrnrlat=min(lats)-30,
urcrnrlat=max(lats)+30,
llcrnrlon=min(lons)-30, urcrnrlon=max(lons)+30, lat_ts=20,
resolution='c')
将函数plt.scatter()
替换为 m.scatter()
。并使用选项latlon=True
应该解决您的问题
...
pts = m.scatter(x,y, marker='o', s=5, c=ts, cmap='plasma', latlon=True)
...
希望这是有用的。