使用cartopy投影在matplotlib动画中分割海岸线

使用cartopy投影在matplotlib动画中分割海岸线,matplotlib,cartopy,Matplotlib,Cartopy,在下面的示例中,网格是在我的海岸线上绘制的。我希望海岸线在顶端(并且仍然使用闪电战)。我得到想要的东西的唯一方法是在init funct中绘制海岸线,然后在update funct中再次绘制它们。因此,我正在为每一帧重新绘制它们。有没有更优雅的方法 import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from cartopy import crs as

在下面的示例中,网格是在我的海岸线上绘制的。我希望海岸线在顶端(并且仍然使用闪电战)。我得到想要的东西的唯一方法是在init funct中绘制海岸线,然后在update funct中再次绘制它们。因此,我正在为每一帧重新绘制它们。有没有更优雅的方法

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from cartopy import crs as ccrs

fig = plt.figure()
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
ax.set_extent([-2,12,-2,12])
ax.coastlines('50m')

mesh = ax.pcolormesh(np.random.randn(10,10))

def init():
    mesh.set_array([])
    return mesh,

def update(t):
    mesh.set_array(np.random.randn(9,9).ravel())
    return mesh,

anim = FuncAnimation(fig, update, 100, init_func=init, blit=True)
plt.show()

诀窍是切换海岸线的可见性

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from cartopy import crs as ccrs

fig = plt.figure()
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
ax.set_extent([-2,12,-2,12])
cl = ax.coastlines('50m')

mesh = ax.pcolormesh(np.random.randn(10,10))

def init():
    mesh.set_array([])
    cl.set_visible(False)
    return mesh, cl

def update(t):
    mesh.set_array(np.random.randn(9,9).ravel())
    cl.set_visible(True)
    return mesh, cl

anim = FuncAnimation(fig, update, 100, init_func=init, blit=True)
plt.show()