首页 > 解决方案 > Matplotlib 动画:如何擦除以前绘制的点?

问题描述

我正在获取和绘制纬度和经度。我可以在情节上对其进行动画处理,但我希望在绘制下一个点之前擦除先前绘制的点。另外我如何打破动画并转到其余代码(例如下面的 elif 语句)这是我的代码结构:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from matplotlib import animation
fig=plt.figure(figsize=(10,8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
ax.gridlines(draw_labels=True, color='black', linestyle='-', alpha=0.2)


def calc(i)

#Here the latitude.degrees and longitude.degrees are calculated (acquired)

plt.scatter(longitude.degrees, latitude.degrees, color='Red', marker='o')


while True:


        if some condition:
          anim = animation.FuncAnimation(fig, calc, interval=100, blit=False)

          plt.show()

        elif some other condition: # I never get to this statement. If i close plot, the entire Gui 
                                   #closes

          some other code

window.close() # I am using pysimplegui

标签: python-3.xmatplotlib

解决方案


如果您之前的轴元素,例如ax.plot(..,..)存储在变量line中。然后您可以按如下方式将其删除:

line = ax.plot(..,..)
for l in line:
    l.remove()
    del l

推荐阅读