我正在创建一个卡通地图上的动画。地图显示卫星以间隔=1秒的间隔绕地球运行。在我的代码中,我使用AnnotationBbox类和add_artist方法在地图上添加了卫星,如下所示。问题是,每隔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()

发布于 2020-04-28 10:32:59
它们的结构意味着它每秒都会向图中添加一个新的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()https://stackoverflow.com/questions/61440147
复制相似问题