def _animate(fig, ax, frame_data, line_width=2, fps=10, tail_color='r',
tail_alpha=0.75, head_color='b', head_alpha=1):
segments, segment_frames, timestamps, fade_frames = frame_data
head_color = np.array(to_rgb(head_color))[None]
tail_color = np.array(to_rgb(tail_color))[None]
# start with all segments transparent
segment_colors = np.zeros((len(segments), 4), dtype=float)
lines = LineCollection(segments, linewidths=line_width, colors=segment_colors)
ax.add_collection(lines)
timer = ax.text(1, 0, '0:00:00', transform=ax.transAxes, zorder=3,
verticalalignment='bottom', horizontalalignment='right',
bbox=dict(facecolor='white'))
def update_frame(frame_idx):
frame_diff = frame_idx - segment_frames
mask = frame_diff < 0
# compute head alpha
alpha1 = 1 - np.minimum(frame_diff/fade_frames, 1)
alpha1[mask] = 0
alpha1 *= head_alpha
# compute tail alpha
alpha2 = (1 - mask) * tail_alpha
# composite alpha and colors
color, alpha = _blend_alpha(head_color, alpha1, tail_color, alpha2)
segment_colors[:, :3] = color
segment_colors[:, 3] = alpha
lines.set_color(segment_colors)
timer.set_text(timestamps[frame_idx])
return lines, timer
interval = 1000. / fps
return FuncAnimation(fig, update_frame, frames=len(timestamps), blit=True,
interval=interval, repeat=True)
评论列表
文章目录