我正在Cartopy地图上创建动画。该地图显示卫星绕地球运行的间隔为1秒。在我的代码中,我使用AnnotationBbox类和add_artist方法作为以下代码在地图上添加了Satellite。问题是每1秒钟,地图更新了一颗新卫星而没有删除旧的卫星,因此它在地图上出现了条纹(如代码所示)。我怎样才能解决这个问题 ?非常感谢您的帮助。
import matplotlib.pyplot as plt
import cartopy.crs as crs
import cartopy
from matplotlib.offsetbox import AnnotationBbox, OffsetImage
from PIL import Image
from skyfield.api import EarthSatellite, Topos, load
import time
from matplotlib.animation import FuncAnimation
###############################################################################
# Get information of satellite
line1 = '1 25544U 98067A 14020.93268519 .00009878 00000-0 18200-3 0 5082'
line2 = '2 25544 51.6498 109.4756 0003572 55.9686 274.8005 15.49815350868473'
satellite = EarthSatellite(line1, line2, name='ISS (ZARYA)')
# cartopy map
fig = plt.figure(figsize=(10, 5))
ax = plt.axes(projection=crs.PlateCarree())
ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)
ax.add_feature(cartopy.feature.LAKES, alpha=0.95)
ax.coastlines()
ax.stock_img()
# Read satellite image
img = Image.open('sat.png')
ax.set_global()
#####################################################################
def animate(i):
# Get coordinate of satellite every 1 second
ts = load.timescale()
t = ts.now()
geometry = satellite.at(t)
subpoint = geometry.subpoint()
lat = subpoint.latitude.degrees
lon = subpoint.latitude.degrees
# Add satellite on the cartopy map
imagebox = OffsetImage(img, zoom=0.03)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, [lat, lon], pad=0, frameon=False)
ax.add_artist(ab)
return ax
ani = FuncAnimation(ax.figure,
animate,
frames=10000,
interval=1000, blit=False, repeat=False)
plt.show()
他们以这种方式构造结构意味着它每秒在图上添加一个新的OffsetImage
和AnnotationBbox
。打算使用FuncAnimation
的方式是,您应该只调整绘图相关部分的基础数据(如位置)。这样的事情应该起作用:
imagebox = OffsetImage(img, zoom=0.03)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, [0, 0], pad=0, frameon=False)
ax.add_artist(ab)
def animate(i):
# Get coordinate of satellite every 1 second
ts = load.timescale()
t = ts.now()
geometry = satellite.at(t)
subpoint = geometry.subpoint()
lat = subpoint.latitude.degrees
lon = subpoint.latitude.degrees
ab.xy = [lon, lat]
return ab,
ani = FuncAnimation(ax.figure,
animate,
frames=10000,
interval=1000, blit=False, repeat=False)
plt.show()