我有从球坐标转换而来的 X、Y 坐标中的数据,我试图在从
Cartopy
获取的地球仪顶部绘制这些数据的轮廓。但它不起作用,我已经尝试了几个小时的谷歌搜索和尝试!任何建议将不胜感激。
我修改了代码以包含虚拟数据,这是要在地球上绘制轮廓和叠加的数据。
下面的链接中也有示例图
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)
#%% Dummy Data
x_num = 10
y_num = 10
num_elements = x_num * y_num
dx = 2
dy = 2
[array_x, array_y] = np.mgrid[0:x_num, 0:y_num]
array_x = (array_x - (x_num- 1) / 2)
array_y = np.flipud((array_y - (y_num - 1) / 2))
array_x = np.transpose(array_x) * dx
array_y = np.flip(np.transpose(array_y)) * dy
#Reshape into two vectors holding the positions. First position is top left moving to top right, Row by Row.
array_x = np.reshape(array_x, (x_num * y_num, -1)).flatten()
array_y = np.reshape(array_y, (x_num * y_num, -1)).flatten()
AF = np.zeros(np.size(u_grid),dtype = np.complex128) #Make sure u_grid and v_grid are the same size
for n in range(num_elements):
kx = array_x[n] * u_grid.ravel()
ky = array_y[n] * v_grid.ravel()
kr = kx+ky
AF = AF + np.exp(1j*kr)
AF = np.resize(AF, np.shape(u_grid))
#AF = np.outer(abs(s),abs(s))
AF = AF / np.max(abs(AF))
AF = 20*np.log10(abs(AF))
AF[AF< -50] =-50
#%% Plots
c = np.linspace(np.min(AF),np.max(AF),50)
)
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
的数据真正映射到地球上的方式。您可能需要验证数据或执行某种数据转换。