matplotlib动画在更新过程中删除行 [英] matplotlib animation removing lines during update

查看:126
本文介绍了matplotlib动画在更新过程中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了地图,正在将纬度和经度CSV格式的数据读取到Pandas DataFrame中。阅读DataFrame后,我成功地使用 for循环绘制了多个大弧。

I've created a map and I am reading in a CSV of latitude and longitude coordinates into a Pandas DataFrame. I've been successful in plotting multiple great arcs using a 'for' loop after reading in the DataFrame.

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

A new great arc is drawn when a new set of coordinates is ADDED to the CSV.

但是,删除坐标后,我不知道如何删除大弧。该线仅停留在地图上。

However, I can't figure out how to REMOVE a great arc once the coordinates have been removed. The line just stays on the map.

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

How do I remove all the old lines and re-draw the lines everytime the CSV is updated? I only want to see the lines currently contained within the CS.

CSV包含以下内容:

The CSV contains the following:

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


推荐答案

使用blitting:



使用blitting时,行会自动删除。

Using blitting:

When using blitting the lines are automatically removed.

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



不发消息:



如果不希望发消息,则可以手动删除行。

Without blitting:

If blitting is not desired, one may remove the lines manually.

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

这篇关于matplotlib动画在更新过程中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆