【问题标题】:matplotlib animation removing lines during updatematplotlib 动画在更新期间删除线条
【发布时间】:2018-08-10 19:25:34
【问题描述】:

我已经创建了一张地图,并且正在将经纬度坐标的 CSV 文件读入 Pandas DataFrame。在读取 DataFrame 后,我已经成功地使用“for”循环绘制了多个大弧线。

当将一组新坐标添加到 CSV 时,将绘制一条新的大弧。

但是,一旦坐标被删除,我无法弄清楚如何删除一个大弧。这条线只是停留在地图上。

如何在每次更新 CSV 时删除所有旧线条并重新绘制线条?我只想查看当前包含在 CS 中的行。

CSV 包含以下内容:

sourcelon   sourcelat   destlon    destlat
50.44        30.51      -80.84      35.22
52.52        13.4       -80.84      35.22
43.18       -22.97      -80.84      35.22
44.1        -15.97      -80.84      35.22
55.44        30.51      -80.84      35.22

最少的代码如下:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.animation


# setup mercator map projection.
fig = plt.figure(figsize=(27, 20))
m = Basemap(projection='mill', lon_0=0)
m.drawcoastlines(color='r', linewidth=1.0)


def animate(i):

    df = pd.read_csv('c:/python/scripts/test2.csv', sep='\s*,\s*',header=0, encoding='ascii', engine='python'); df 


    for x,y,z,w in zip(df['sourcelon'], df['sourcelat'], df['destlon'], df['destlat']):
        line, = m.drawgreatcircle(x,y,z,w,color='r')



ani = matplotlib.animation.FuncAnimation(fig, animate, interval=1000)

plt.tight_layout()
plt.show()

【问题讨论】:

    标签: python pandas animation matplotlib


    【解决方案1】:

    使用 blitting:

    使用 blitting 时,线条会自动删除。

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    import matplotlib.animation
    
    
    # setup mercator map projection.
    fig = plt.figure(figsize=(13, 8))
    m = Basemap(projection='mill', lon_0=0)
    m.drawcoastlines(color='grey', linewidth=1.0)
    
    def get_data():
        a = (np.random.rand(4,2)-0.5)*300
        b = (np.random.rand(4,2)-0.5)*150
        df= pd.DataFrame(np.concatenate((a,b),axis=1),
                         columns=['sourcelon','destlon','sourcelat','destlat'])
        return df
    
    def animate(i):
        df = get_data()
        lines = []
        for x,y,z,w in zip(df['sourcelon'], df['sourcelat'], df['destlon'], df['destlat']):
            line, = m.drawgreatcircle(x,y,z,w,color='r')
            lines.append(line)
        return lines
    
    ani = matplotlib.animation.FuncAnimation(fig, animate, interval=1000, blit=True)
    
    plt.tight_layout()
    plt.show()
    

    没有blitting:

    如果不需要 blitting,可以手动删除这些行。

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    import matplotlib.animation
    
    
    # setup mercator map projection.
    fig = plt.figure(figsize=(13, 8))
    m = Basemap(projection='mill', lon_0=0)
    m.drawcoastlines(color='grey', linewidth=1.0)
    
    def get_data():
        a = (np.random.rand(4,2)-0.5)*300
        b = (np.random.rand(4,2)-0.5)*150
        df= pd.DataFrame(np.concatenate((a,b),axis=1),
                         columns=['sourcelon','destlon','sourcelat','destlat'])
        return df
    
    lines = []
    
    def animate(i):
        df = get_data()
        for line in lines:
            line.remove()
            del line
        lines[:] = []
        for x,y,z,w in zip(df['sourcelon'], df['sourcelat'], df['destlon'], df['destlat']):
            line, = m.drawgreatcircle(x,y,z,w,color='r')
            lines.append(line)
    
    ani = matplotlib.animation.FuncAnimation(fig, animate, interval=1000)
    
    plt.tight_layout()
    plt.show()
    

    【讨论】:

    • 感谢您的回答!做到了。结果我发现了别的东西……有没有办法防止地块纵向缠绕?
    • 有没有办法重新缩放/自动缩放轴with blitting?不寻找细节,-是的,不,简单,困难就可以了。
    • @wwii 原则上你可以 blit 任何你想要的东西,但你需要自己实现它(FuncAnimation 之外)。缩放轴并随后绘制轴刻度相当昂贵,因此在对自动缩放轴进行 blitting 时,最终可能不会获得太多收益。所以总的来说,我会说,“可能,中等复杂”作为这里的简短回答。 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2016-06-16
    • 1970-01-01
    • 2013-08-25
    相关资源
    最近更新 更多