matplotlib - Cartopy 纵横比匹配线子图与地理地图
问题描述
我有两个地块;地图和线图
我希望线图的高度与地理地图的高度相匹配。有没有办法从 cartopy geoaxes 获得纵横比?如果我执行 ax1.get_aspect(),它会返回“等于”。
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ds = xr.tutorial.open_dataset('air_temperature')['air'].isel(time=0)
plt.figure(figsize=(15, 10))
ax1 = plt.subplot(121, projection=ccrs.PlateCarree())
ds.plot(transform=ccrs.PlateCarree(), ax=ax1, add_colorbar=False)
ax2 = plt.subplot(122)
ax2.plot([1, 2, 3], [5, 6, 7])
最终编辑:我看错了;分频器和斧头是有区别的。我不知道您可以从分隔线中生成多个轴。
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig = plt.figure(figsize=(13, 8))
ax1 = fig.add_subplot(111, projection=ccrs.PlateCarree())
img = xr.tutorial.open_dataset('air_temperature')['air'].isel(time=0).plot(
x='lon', y='lat', ax=ax1, transform=ccrs.PlateCarree(), add_colorbar=False)
ax1.coastlines()
ax1.set_title('ax1')
divider = make_axes_locatable(ax1)
ax2 = divider.new_horizontal(size="10%", pad=0.1, axes_class=plt.Axes)
fig.add_axes(ax2)
plt.colorbar(img, cax=ax2)
ax3 = divider.new_horizontal(size="100%", pad=1, axes_class=plt.Axes)
fig.add_axes(ax3)
ax3.plot([1, 2, 3], [5, 6, 7])
解决方案
我将使用mpl_toolkits.axes_grid1.make_axes_locatable
类似于在颜色条相对于地理轴(cartopy)的正确放置颜色条中的完成方式。不同之处在于您将为绘图创建轴,而不是为颜色条创建轴。
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig = plt.figure(figsize=(13, 8))
ax1 = fig.add_subplot(111, projection=ccrs.PlateCarree())
ax1.coastlines()
divider = make_axes_locatable(ax1)
ax2 = divider.new_horizontal(size="100%", pad=0.4, axes_class=plt.Axes)
fig.add_axes(ax2)
ax2.plot([1, 2, 3], [5, 6, 7])
plt.show()
推荐阅读
- javascript - 类不是构造函数,多个'require'
- javascript - 我的光线投射程序中出现奇怪的失真
- r - 如何使用字符/长字符串数据折叠和聚合行
- scala - 编译器未“选择”辅助构造函数
- sql - 根据选择中的计数列返回最大行
- php - Magento 2 Collection Date 按一小时过滤
- python - 如何从 Django 模板创建指向我的 React 应用程序的链接
- c++ - 如果在 DLL 和调用应用程序中使用 GPGPU API 会发生什么
- selenium - 在方法外将 Webdriver 声明为 null 并在方法内声明 Webdriver 时出现空指针异常
- angular - 使用索引编辑单击行中的特定元素