【问题标题】:Remove old artist in FuncAnimation of Matplotlib删除 Matplotlib 的 FuncAnimation 中的旧艺术家
【发布时间】:2020-04-26 11:43:49
【问题描述】:

我正在地图上创建动画。 该地图显示卫星以间隔 = 1s 绕地球运行。 在我的代码中,我使用 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()

【问题讨论】:

  • def animate(i): plt.cla() ...我认为将其添加到代码中会很好。
  • plt.cla() 删除我的背景图,只保留卫星

标签: python animation cartopy satellite


【解决方案1】:

您拥有这种结构化的方式意味着它每秒都会在情节中添加一个新的OffsetImageAnnotationBboxFuncAnimation 的使用方式是您应该只调整绘图相关部分的基础数据(如位置)。这样的事情应该可以工作:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-19
    • 2022-11-11
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多