I am making a scatter plot of positions marked with latitude and longitude which works all right as it shows all the positions in a time period in a static image. But I was wondering if there is any easy way of utilizing that I have the unixtime to every position - so I can show the movements as a timelapse - kind of looping through the positions and showing an animation of the movement.
EDIT:
I have set up a dynamicly updating plot that plots all the positions one by one, now I just have to add the basemap in the background. The codes come from this answer.
import matplotlib.pyplot as plt
import sqlite3 as lite
from operator import itemgetter
def getData():
con = lite.connect('database.db')
with con:
cur = con.cursor()
cur.execute('SELECT latitude, longitude, unixtime FROM Message WHERE latitude > 50 AND longitude > -30 AND longitude < 40 AND latitude < 80')
all_rows = [[int(x[0]), int(x[1]), int(x[2])] for x in cur]
all_rows = sorted(all_rows, key=itemgetter(2))
return all_rows
plt.ion()
class DynamicUpdate():
#Suppose we know the x range
min_x = 0
max_x = 10000
def on_launch(self):
#Set up plot
self.figure, self.ax = plt.subplots()
self.lines, = self.ax.plot([],[], 'o')
#Autoscale on unknown axis and known lims on the other
self.ax.set_autoscaley_on(True)
self.ax.set_xlim(-50, 50)
self.ax.set_ylim(40, 80)
#Other stuff
self.ax.grid()
def on_running(self, xdata, ydata):
#Update data (with the new _and_ the old points)
self.lines.set_xdata(xdata)
self.lines.set_ydata(ydata)
#Need both of these in order to rescale
self.ax.relim()
self.ax.autoscale_view()
#We need to draw *and* flush
self.figure.canvas.draw()
self.figure.canvas.flush_events()
#Example
def __call__(self):
import numpy as np
import time
self.on_launch()
xdata = []
ydata = []
all_rows = getData()
for x in all_rows:
a,b,f = zip(x)
xdata.append(b)
ydata.append(a)
self.on_running(xdata, ydata)
return xdata, ydata
d = DynamicUpdate()
d()
Old Code:
This shows the static data
map = Basemap(projection='merc', lat_0=59.45, lon_0=10.5,
resolution = 'c', area_thresh = 1000,
llcrnrlon=-30, llcrnrlat=50,
urcrnrlon=40, urcrnrlat=80)
map.drawcoastlines()
map.fillcontinents(color='black')
con = lite.connect('database.db')
with con:
cur = con.cursor()
cur.execute('SELECT latitude, longitude FROM Message WHERE latitude > 50 AND longitude > -30 AND longitude < 40 AND latitude < 80')
data = cur.fetchall()
y,x = zip(*data)
x,y = map(x,y)
plt.scatter(x,y, s=0.07, alpha=0.6, color="#e74c3c", edgecolors='none')
plt.show()