在以下示例中,网格绘制在我的海岸线之上。我希望海岸线位于顶部(并且仍然使用blitting)。我设法得到我想要的唯一方法是在init func中绘制海岸线,然后在更新功能中再次绘制它们。所以我为每一帧重新绘制它们。有更优雅的方式吗?
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()
答案 0 :(得分:1)
诀窍是切换海岸线的可见度。
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()