I've searched high and low but cannot find another answer to this question.
I'm trying to visualise output from the Met Office's Unified Model over the Antarctic Peninsula. At the moment, I have a longitudinal cross section, with potential temperature plotted as filled contours. I would like to plot wind vectors on top of this so they look more like:
I've tried to adapt the examples for lat/lon coordinates given in matplotlib's examples (quiver_demo for instance), but with no luck (probably because I'm doing something wrong that is blindingly obvious).
I have the u, v and w components of wind (although I don't need v because I have taken a slice through one latitude line).
I'm currently using a module called iris (v1.7), which allows me to deal with the files I have (e.g. to convert from a rotated pole projection and convert latitudes/longitudes to sensible coordinates), so please excuse unfamiliar lines in my code.
The code I have is as follows:
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()
However, when the vectors are plotted they appear as lines:
I am not sure whether this is because they are too clustered together (when I change line properties like linewidth and scale, the vectors go pretty crazy) or because my inputs are incorrect. The u and w variables are 2D arrays (lon/height) containing the westerly and upward component of wind, respectively, so should contain the magnitude of movement in these directions.
Apologies that I cannot post the actual files: they are in a specific Met Office .pp format (of course) but have similar qualities to netCDF. I have tried to describe the data dimensions in the code comments.