【问题标题】:Cartopy aspect ratio match line subplots to geographic mapCartopy 纵横比匹配线子图与地理地图
【发布时间】:2019-06-12 22:11:45
【问题描述】:

我有两个地块;地图和线图

我希望线图的高度与地图的高度相匹配。有没有办法从 cartopy geoaxes 获得纵横比?如果我执行 ax1.get_aspect(),它会返回 'equal'。

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])

【问题讨论】:

  • 最简单的方法是使用像figsize=(15, 4) 这样的smth 并使用最后一个数字直到合适为止。
  • 不幸的是,我需要全部自动化;所以我想找到地图的实际纵横比并重新调整线条的纵横比。

标签: matplotlib cartopy


【解决方案1】:

我会使用mpl_toolkits.axes_grid1.make_axes_locatable,类似于在Correct placement of colorbar relative to geo axes (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()

【讨论】:

  • 哇!好主意!如果您有 4 个子图,或者只是地图的颜色条怎么办?它似乎不允许连续添加。 ``` fig = plt.figure(figsize=(13, 8)) ax1 = fig.add_subplot(111, projection=ccrs.PlateCarree()) ax1.coastlines() divider = make_axes_locatable(ax1) ax2 = divider.new_horizo​​ntal( size="100%", pad=0.4, axes_class=plt.Axes) fig.add_axes(ax2) ax2.plot([1, 2, 3], [5, 6, 7]) divider = make_axes_locatable(ax2) ax3 = divider.new_horizo​​ntal(size="100%", pad=0.4, axes_class=plt.Axes) fig.add_axes(ax3) ax3.plot([1, 2, 3], [5, 6, 7]) plt.显示()```
  • 不,你仍然需要使用相同的分隔线,而不是创建一个新的。
  • 如果我有 4 个子图,有两个带颜色条的地图和两个线图;我认为这行不通。
  • 我想不出它不起作用的原因,所以试试看会发生什么。
  • 对不起,我的意思是,是的,我们可以在多个子图上使用这种方法;但是,如果我们使用这个 make_axes_locatable 方法添加一个颜色条,我们就不能添加相同的纵横比线图而没有时髦的行为。
猜你喜欢
  • 1970-01-01
  • 2013-03-06
  • 2020-07-03
  • 2013-12-10
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
相关资源
最近更新 更多