geeb.24
geeb.24

Reputation: 557

Matplotlib Basemap Plotting Lat/Lons

I cannot for the life of me figure out how to animate points (earthquake epicenters) on a Matplotlib basemap plot, using the animation function. I have tried implementing this example code into my script, but all my points plot at once. It may have something to do with my function and the for loop within the function. Any thoughts?

Here is my code:

#!/usr/local/bin/python
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

fig = plt.figure()


#Build the basemap
antmap = Basemap(projection='spstere', boundinglat=30, lon_0=-60, resolution='f')
antmap.drawcoastlines(color='black', linewidth=0.15)
antmap.bluemarble()

eq_data = open('eq_data')
lats, lons = [], []
mag = []
x,y = antmap(0,0)
point = antmap.plot(x,y,'ro', markersize=5)[0]

def init():
    point.set_data([],[])
    return point,

# Begin Animation
def animate(i):
    for i, line in enumerate(eq_data.readlines()):
        lats.append(float(line.split(',')[0]))
        lons.append(float(line.split(',')[1]))
        mag.append(float(line.split(',')[2]))
        x,y = antmap(lons, lats)
        point.set_data(x, y)
    return point,
#    antmap.plot(x,y, 'ro', markersize=8)
ani = animation.FuncAnimation(fig, animate, init_func=init, interval=500, blit=False)
plt.show()

How would I animate these points? I haven't been able to find anything else online thus far that has been able to animate these earthquake epicenters. Thanks.

UPDATED AND CORRECTED SCRIPT

#!/usr/local/bin/python
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np

# Create the Figure
fig = plt.figure()


#Build the basemap
antmap = Basemap(projection='spstere', boundinglat=30, lon_0=-60, resolution='l')
antmap.drawcoastlines(color='black', linewidth=0.15)
#antmap.bluemarble()

eq_data = open('eq_data')
lats, lons = [], []
mag = []
x,y = antmap(lons,lats)

for i, line in enumerate(eq_data.readlines()):
    lats.append(float(line.split(',')[0]))
    lons.append(float(line.split(',')[1]))
    mag.append(float(line.split(',')[2]))
eq_data.close()

antmap.plot(x,y,'ro', markersize=5)

# Begin Animation
def animate(i):
    x,y = antmap(lons[i], lats[i])
    antmap.plot(x,y,'ro', markersize=8)

animation = FuncAnimation(fig, animate,  interval=100, blit=False)
plt.show()

Upvotes: 2

Views: 643

Answers (1)

tmdavison
tmdavison

Reputation: 69106

I think you want to build your lats and lons lists outside of the animate function, then just plot one point at a time inside the function.

animate is called sequentially with an increasing i, so we can use that to index the lons and lats lists.

(Note, I haven't tested this, but I think it should work)

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

fig = plt.figure()

#Build the basemap
antmap = Basemap(projection='spstere', boundinglat=30, lon_0=-60, resolution='f')
antmap.drawcoastlines(color='black', linewidth=0.15)
antmap.bluemarble()

eq_data = open('eq_data')
lats, lons = [], []
mag = []
x,y = antmap(0,0)
point = antmap.plot(x,y,'ro', markersize=5)[0]

# Read the data and build the lists of coordinates here
for i, line in enumerate(eq_data.readlines()):
    lats.append(float(line.split(',')[0]))
    lons.append(float(line.split(',')[1]))
    mag.append(float(line.split(',')[2]))
eq_data.close()

def init():
    point.set_data([],[])
    return point,

# Begin Animation
def animate(i):
    x,y = antmap(lons[i], lats[i])
    point.set_data(x, y)
    return point,

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=500, blit=False)
plt.show()

Upvotes: 3

Related Questions