I am trying to create a cross-section plot using MetPy and xarray for a variable stored in a data xarray.Dataset. However, the cross-section line appears curved when using longitude-latitude pairs, as shown in the attached image. I would like to make a straight-line cross-section instead.
Here's the code snippet for parsing the data and creating the cross-section:
# Parse the data as MetPy for cross-sectional analysis
data = data.metpy.parse_cf()
# Longitude-latitude pair for cross-section analysis
start = (30.0, -90.0)
end = (60, 60.0)
# Create the cross-section
cross = cross_section(data, start, end).set_coords(('lat', 'lon'))
And this is the code for plotting:
# Preliminary plot
fig = plt.figure(1, figsize=(8, 6))
ax = plt.axes()
# Plot using contourf
sf_contour = ax.contourf(cross['lon'], cross['time'], cross['sf_ano'],
levels=np.arange(-3.4, 3.8, .4)*1e7, cmap='RdBu_r',
extend='both')
sf_colorbar = fig.colorbar(sf_contour)
# Adjust the y-axis
ax.set_ylim(cross['time'].max(), cross['time'].min())
I've also attached an image showing the current output, where the cross-section line is curved. How can I make this a straight line cross section?
MetPy's
cross_sectionfunction always takes the slice along the geodesic ("great circle") between the start and end points in lat/lon space. This represents the shortest path between these points in true lat/lon space (points on a spheroid).MetPy doesn't currently support performing cross-sections in projected space. If that's important to you, I'd suggest opening a feature request or, even better, contributing a Pull Request.