Plot a 2-d line with a gradient representing ordering.
  See http://stackoverflow.com/q/8500700/10601 for details.

def gradient_line(xs, ys, colormap_name='jet', ax=None):
  '''Plot a 2-d line with a gradient representing ordering.
  See http://stackoverflow.com/q/8500700/10601 for details.'''
  if ax is None:
    ax = plt.gca()
  cm = plt.get_cmap(colormap_name)
  npts = len(xs)-1
  colors = cm(np.linspace(0, 1, num=npts))
  if hasattr(ax, 'set_prop_cycle'):
    ax.set_prop_cycle(color=colors)
  else:
    ax.set_color_cycle(colors)
  for i in range(npts):
    ax.plot(xs[i:i+2],ys[i:i+2])
  return plt.show