使用散射和使用set_offsets的matplotlib中的动画:如何更新散点的颜色

时间:2015-09-29 15:09:38

标签: python animation matplotlib scatter-plot

我正在尝试创建一个动画,用于绘制跑步中的GPS数据。我有代码绘制点并创建动画。为了删除旧点并使用每个帧绘制新值,我使用scat.set_offsets(x[i],y[i])绘制点。我遇到的麻烦是我想要点的颜色来反映那个位置的速度。下面的代码有效但我无法弄清楚颜色更新。我尝试过使用scat.alpha(),但它会切换一次颜色,而不会再次更新。我想使用喷射,例如,0英里/小时是蓝色,最快的时间是红色。

正在运行的日志的前两列为lon/lat,第三列为velocity。一些数据:

 Longitude   Latitude   GPS Speed(km/h)

-88.19895578    43.19975045 0

-88.19894667    43.19976286 0

-88.19893236    43.19977277 0

-88.19872605    43.19969481 9.9

-88.19871956    43.19967161 10.799999

-88.19872339    43.19962663 11.7

-88.19873801    43.19959561 11.7

-88.19879232    43.19958254 9.9

-88.19876674    43.1995382  9.9

-88.19876797    43.19948614 7.2

代码如下。谢谢你的帮助!

"""
Matplotlib Animation of Accelerometer Vectors

author: Ryan Croke
website: http://TheHolyMath.com
"""

import numpy as np
import networkx as nx
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib  import cm
import csv
import json
import smopy
import re

### DATA ###
# Read shape file
g = nx.read_shp("tl_2014_us_state/tl_2014_us_state.shp")

# plot points of acceleration and deceleration
f = open('runninglog.csv')
cr = csv.reader(f)

# Make arrays for processing
latitude = []
longitude = []
velocity = []

# Velocity is in m/s - convert to miles/hour
# 1m/s = 2.236 mph
for row in cr:
    # Need to test for headers - pass anything with letters
    if any(c.isalpha() for c in row[0]) == True:
        pass
    else:
        longitude.append(float(row[0]))

    if any(c.isalpha() for c in row[1]) == True:
        pass
    else:
        latitude.append(float(row[1]))

    if any(c.isalpha() for c in row[2]) == True:
        pass
    else:
        velocity.append(round(float(row[2])*2.236,2))

# Numer of plots
P = int(len(latitude))

# In the USA longitude is decreasing from west to east, latitude is increasing from south to north
# lower left will be max(lon) and min(lat)
pos1 = (float(min(latitude)),float(max(longitude)))
pos2 = (float(max(latitude)),float(min(longitude)))

### Animation ###
# First set up the figure, the axis, and the plot element we want to animate
map = smopy.Map(pos1, pos2, z=15)
# Smoopy allows you to create a matplotlib image using a t0_pixels command
x = []
y = []

# Create array's for lat/lon in pixel image
for i in xrange(len(latitude)):
    x1, y1 = map.to_pixels(latitude[i],longitude[i])
    x.append(x1)
    y.append(y1)

# animation of a scatter plot using x, y from above
#------------------------------------------------------------------------------

fig = plt.figure()
ax = map.show_mpl(figsize=(8, 6))
scat = ax.scatter([],[],s=60)

# initialization function: plot the background of each frame
def init():
    scat.set_offsets([])
    #scat.set_facecolor([])
    return scat,

# animation function.  This is called sequentially
def animate(i):
    scat.set_offsets([x[i], y[i]])
    #scat.set_edgecolors('red')
    #scat.set_facecolor()

    return scat,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=P, interval=40, repeat=False, blit=True)

# save the animation as an mp4.  This requires ffmpeg or mencoder to be
# installed.  The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5.  You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
anim.save('Around_the_block_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show()

0 个答案:

没有答案