matplotlib animation removing lines during update

2019-08-28 05:26发布

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.

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.

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.

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

The minimal code is below:

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

1条回答
狗以群分
2楼-- · 2019-08-28 06:05

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()
查看更多
登录 后发表回答