带 cartopy 的旋转地球 GIF

问题描述 投票:0回答:1

我未能找到最有效的方法来使用 cartopy 生成具有填充轮廓的旋转地球仪的简单动画。下面的代码生成一个静态 gif,可能是因为该图形没有自行重绘?有没有办法让动画函数只改变地理投影,而无需再次调用

contourf()
(这在计算上是昂贵的)?

from pylab import *
import cartopy.crs as ccrs
from matplotlib.animation import FuncAnimation

lon, lat = meshgrid(
    (linspace(-180,180,361)+0.5)[::4],
    (linspace(-90,90,181)+0.5)[::4],
    )   

h = (abs(lon)<20).astype(int) * (abs(lat)<10).astype(int)

fig = figure(figsize=(3,3))
ax = fig.add_subplot(1, 1, 1, projection = ccrs.Orthographic())
ax.contourf(lon, lat, h, transform=ccrs.PlateCarree())

def update_fig(t):
    ax.projection = ccrs.Orthographic(t)

ani = FuncAnimation(
    fig,
    update_fig,
    frames = linspace(0,360,13)[:-1],
    interval = 100,
    blit = False,
    )

ani.save('mwe.gif')
python cartopy matplotlib-animation
1个回答
0
投票

值得注意的是,您的代码实际上并不会生成静态 gif。如果您向轴添加边框(使用

ax.coastlines()
),那么您会看到地球仪每帧都在更新,您的数据只是没有更新以匹配新的投影。

如果您使用

ax.pcolormesh()
,则可以解决此问题,如下所示:

from pylab import *
import cartopy.crs as ccrs
from matplotlib.animation import FuncAnimation

lon, lat = meshgrid(
    (linspace(-180,180,361)+0.5)[::4],
    (linspace(-90,90,181)+0.5)[::4],
    )   

h = (abs(lon)<20).astype(int) * (abs(lat)<10).astype(int)

fig = figure(figsize=(3,3))
ax = fig.add_subplot(1, 1, 1, projection = ccrs.Orthographic())
ax.coastlines()
mesh = ax.pcolormesh(lon, lat, h, transform=ccrs.PlateCarree())

def update_fig(t):
    ax.projection = ccrs.Orthographic(t)
    mesh.set(transform=ccrs.PlateCarree())

ani = FuncAnimation(
    fig,
    update_fig,
    frames = linspace(0,360,13)[:-1],
    interval = 200)

不幸的是,

ax.contourf
生成的对象似乎不能以这种方式工作。我能找到的最接近的是
mesh.set_array()
,它在 cartopy 文档中用于
pcolormesh
,但也适用于
contourf
。对于
.set_transform()
生成的对象,没有
.set()
contourf

© www.soinside.com 2019 - 2024. All rights reserved.