我有从球坐标转换而来的 X,Y 坐标中的数据,我试图在我从
Cartopy
获取的地球仪顶部绘制这些数据的轮廓。但它不起作用,我已经尝试了几个小时的谷歌搜索和尝试!任何建议将不胜感激。
代码在这里,
numpy
数据文件在这里。
下面的链接中也有示例图
import numpy as np
import matplotlib.pyplot as plt
# import cartopy.feature as cf
import cartopy.crs as ccrs
import cartopy.feature as cf
plt.close('all')
#%% Co-ordindate System
resolution = 201
theta = np.linspace(-90, 90, num=resolution) * np.pi/180
phi = np.linspace(0, 360, num=resolution) * np.pi/180
theta_grid, phi_grid = np.meshgrid(theta,phi)
u_grid = np.sin(theta_grid) * np.cos(phi_grid)
v_grid = np.sin(theta_grid) * np.sin(phi_grid)
#%% Load Numpy array of data
AF = np.load('Plotted_data.npy')
c = np.arange(np.min(AF),np.max(AF),0.25)
#%% Plots
fig = plt.figure('UV')
ax = fig.add_subplot(111)
plt.contourf(u_grid,v_grid,AF,c,extend="both", cmap = 'Spectral_r') #plasma #gnuplot2
plt.colorbar(label ='Directivity (dBi)')
ax.set_xlabel('u = sin(theta)cos(phi)')
ax.set_ylabel('v = sin(theta)sin(phi)')
ax.legend(loc=1)
projection=ccrs.Geostationary(central_longitude=0.0, satellite_height=35785831)
fig = plt.figure('No Overlay')
ax = plt.axes(projection=projection)
ax.add_feature(cf.COASTLINE)
plt.show()
fig = plt.figure('Attempt to Overlay')
ax = plt.axes(projection=projection)
plt.contourf(u_grid,v_grid,AF,c,extend="both", cmap = 'Spectral_r', alpha = 0.2) #plasma #gnuplot2
ax.add_feature(cf.COASTLINE)
plt.show()
数据示例图以及未能将 countour 覆盖到地球上
球坐标 XY 变换中的数据等高线图:
我想在其上绘制轮廓的地球仪:
我已经尝试过多次谷歌搜索这个问题,
Cartopy
网站,stackexchange
上的帖子,但我还没有得到这个工作......非常感谢任何建议。
由于您想将等高线图与地理地图一起显示,因此您必须使用适当的坐标,即经度和纬度(以度为单位),就像许多 Cartopy 示例中一样,例如在 this one 中,他们将原始值(以弧度为单位)转换为度数。在您的代码中,它可能类似于
theta_deg = np.rad2deg(theta)
和 phi_deg = np.rad2deg(phi)
。另外,您还必须在 transform
中设置 matplotlib.pyplot.contourf()
参数。这是使用您的数据的简化示例:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
#co-ordindate system
resolution = 201
lats = np.linspace(-90, 90, num=resolution)
lons = np.linspace(0, 360, num=resolution)
lons, lats = np.meshgrid(lons, lats)
# load numpy array of data
AF = np.load('Plotted_data.npy')
projection=ccrs.Geostationary(central_longitude=0.0, satellite_height=35785831)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=projection)
# show coastlines
ax.coastlines()
# add color filled contours
filled_c = ax.contourf(lons, lats, AF, transform=ccrs.PlateCarree(), extend="both", cmap='Spectral_r', alpha=0.2)
plt.show()
结果:
可能它看起来并不完全如你所愿,尤其是。那个丑陋的突然转变恰好发生在本初子午线。但这就是使用地理坐标时来自
Plotted_data.npy
的数据真正映射到地球上的方式。您可能需要验证数据或执行某种数据转换。