首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >删除Matplotlib的FuncAnimation中的旧艺术家

删除Matplotlib的FuncAnimation中的旧艺术家
EN

Stack Overflow用户
提问于 2020-04-26 19:43:49
回答 1查看 422关注 0票数 0

我正在创建一个卡通地图上的动画。地图显示卫星以间隔=1秒的间隔绕地球运行。在我的代码中,我使用AnnotationBbox类和add_artist方法在地图上添加了卫星,如下所示。问题是,每隔1秒,地图就会更新一颗新的卫星,而不会删除旧的卫星,因此它会在地图上生成一个条纹,作为图片(代码下方)。我该如何解决这个问题呢?非常感谢你的帮助。

代码语言:javascript
运行
复制
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()

EN

回答 1

Stack Overflow用户

发布于 2020-04-28 10:32:59

它们的结构意味着它每秒都会向图中添加一个新的OffsetImageAnnotationBbox。使用FuncAnimation的方式是调整绘图相关部分的底层数据(如位置)。像这样的东西应该是有效的:

代码语言:javascript
运行
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61440147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档