我正在创建以下地图。
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
# Create a plot using the PlateCarree projection
fig, ax = plt.subplots(figsize=(10, 6), subplot_kw={'projection': ccrs.PlateCarree()})
# Set the extent for the Amazon region in regular latitude/longitude coordinates
ax.set_extent([-74, -32, -5, 15], crs=ccrs.PlateCarree())
# Add features to the map
ax.coastlines()
ax.add_feature(cfeature.BORDERS, linestyle=':')
# Add gridlines with labels
gl = ax.gridlines(draw_labels=True, linestyle='--', color='gray')
gl.top_labels = False # Disable top labels
gl.right_labels = False # Disable right-side labels
gl.xlabel_style = {'size': 10, 'color': 'black'}
gl.ylabel_style = {'size': 10, 'color': 'black'}
# Display the map
plt.show()
但我想通过给出此框的坐标来旋转视图,以使 x 轴与海岸方向一致。
嗯,我可以找到解决方案,但想了解一些命令。
# Define projection parameters
projection_center = (280, 25.5) # (longitude, latitude)
azimuth = 165 # azimuth of the oblique equator in degrees
# Create an Oblique Mercator projection
oblique_mercator = ccrs.ObliqueMercator(central_longitude=projection_center[0],
central_latitude=projection_center[1],
azimuth=azimuth)
# Create a new figure and axis
fig, ax = plt.subplots(figsize=(6, 4), subplot_kw={'projection': oblique_mercator})
# Set the extent for the Amazon region in regular latitude/longitude coordinates
_,ymin = oblique_mercator.transform_point(-60, 0, ccrs.PlateCarree())
_,ymax = oblique_mercator.transform_point(-40, 5, ccrs.PlateCarree())
xmin,_ = oblique_mercator.transform_point(-75, 5, ccrs.PlateCarree())
xmax,_ = oblique_mercator.transform_point(-40, -10, ccrs.PlateCarree())
ax.set_extent([xmin, xmax, ymin, ymax], crs=oblique_mercator)
# Add features to the map
ax.coastlines()
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.gridlines()
ax.invert_yaxis() # for some reason
ax.invert_xaxis() # for some reason
# Add gridlines with labels
gl = ax.gridlines(draw_labels=True, linestyle='--', color='gray')
gl.top_labels = False # Disable top labels
gl.right_labels = False # Disable right-side labels
gl.xlabel_style = {'size': 10, 'color': 'black'}
gl.ylabel_style = {'size': 10, 'color': 'black'}
我基本上玩过
projection_center
和azimuth
,但不明白如何根据我想要旋转地图的程度来获取它们。谁能解释一下吗?
我也不知道为什么我必须反转 x 和 y 轴。