我已经搜索过各种资料,但是没有找到关于这个问题的答案。
我正在尝试将英国气象局的统一模型在南极半岛上的输出可视化。目前,我有一个经度剖面图,填充轮廓中绘制了潜在温度。我想在其上方绘制风向矢量,使其看起来更像:
我尝试调整matplotlib示例(例如quiver_demo)中给出的纬/经度坐标的示例,但没有成功(可能是因为我做错了一些明显的事情)。
我有风的u、v和w分量(虽然我不需要v,因为我已经从一个纬度线切片中取得了数据)。
我目前正在使用一个名为iris(v1.7)的模块,它允许我处理我拥有的文件(例如将旋转极投影转换为合理的坐标系中的纬度/经度),所以请原谅我的代码中不熟悉的行。
我现在的代码如下:
import iris
import iris.plot as iplt
import matplotlib.pyplot as plt
import numpy as np
# Load variables from file
fname = ['/pathname/filename.pp']
theta= iris.load_cube(fname,'air_potential_temperature') #all data are 3D 'cubes' of dimensions [x,y,z] = [40,800,800]
U = iris.load_cube(fname, 'eastward_wind')
W = iris.load_cube(fname, 'upward_air_velocity')
lev_ht = theta.coord('level_height').points # for extracting z coordinate only - shape = (40,)
##SPATIAL COORDINATES
## rotate projection to account for rotated pole
var_name = theta
pole_lon = 298.5
pole_lat = 22.99
rotated_lat = var_name.coord('grid_latitude').points
rotated_lon = var_name.coord('grid_longitude').points
real_lon, real_lat = iris.analysis.cartography.unrotate_pole(rotated_lon,rotated_lat, pole_lon, pole_lat)
#define function to find index of gridbox in question
def find_gridbox(x,y):
global lon_index, lat_index
lat_index = np.argmin((real_lat-x)**2) #take whole array and subtract lat you want from each point, then find the smallest difference
lon_index = np.argmin((real_lon-y)**2)
return lon_index, lat_index
lon_index, lat_index = find_gridbox(-66.58333, -63.166667) # coordinates of Cabinet Inlet
#add unrotated lats/lons into variables
lat = var_name.coord('grid_latitude')
lon = var_name.coord('grid_longitude')
New_lat = iris.coords.DimCoord(real_lat, standard_name='latitude',long_name="grid_latitude",var_name="lat",units=lat.units)
New_lon= iris.coords.DimCoord(real_lon, standard_name='longitude',long_name="grid_longitude",var_name="lon",units=lon.units)
var_names = [theta, U, W]
for var in var_names:
var.remove_coord('grid_latitude')
var.add_dim_coord(New_lat, data_dim=1)
var.remove_coord('grid_longitude')
var.add_dim_coord(New_lon, data_dim=2)
# Create 2D subsets of data for plotting (longitudinal transects)
theta_slice = theta[:,lat_index,:]
U_slice = U[:,lat_index,:]
W_slice = W[:,lat_index,:]
# Create 2d arrays of lon/alt for gridding
x, y = np.meshgrid(lon.points, lev_ht)
u = U_slice
w = W_slice
# plot
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_axis_bgcolor('k') #set orography to be black
ax3 = iplt.contourf(theta_slice, cmap=plt.cm.bwr)
ax1.set_ylim(0,5500)
barbs = plt.quiver(x, y, u, w)
ax3.set_clim(vmin=250, vmax=320)
ax1.set_title('25 May 00:00 UTC', fontsize=28)
ax1.set_ylabel('Height (m)', fontsize=22)
ax1.set_xlabel('Longitude', fontsize=22)
ax1.tick_params(axis='both', which='major', labelsize=18)
plt.colorbar(ax3)
plt.show()
然而,当向量被绘制时,它们会出现为线条形式:
我不确定这是因为它们太靠近一起了(当我改变线宽和比例等属性时,向量就会非常混乱),还是因为我的输入有误。u和w变量是2D数组(经度/高度),包含风向的西向和上升分量,因此应包含在这些方向上的移动大小。
很抱歉我无法发布实际文件:它们是特定Met Office .pp格式的(当然),但与netCDF具有类似的特性。我已经尝试在代码注释中描述数据维度。
zeros_like
复制x,y,u,w
,然后在中间添加一个箭头,看看是否能够合理绘制。 - cphlewis