我最近以类似的要求回答了一个问题(使用matplotlib创建了20多种独特的图例颜色)。我在那里展示了您可以映射将线条绘制到颜色图所需的颜色循环。您可以使用相同的过程为每对点获取特定的颜色。
您应该仔细选择颜色图,因为如果颜色图是彩色的,则沿线的颜色过渡可能会显得很剧烈。
或者,您可以更改每个线段的Alpha,范围从0到1。
下面的代码示例中包含一个例程(highResPoints),用于扩展您的随机游走所拥有的点数,因为如果您的点数太少,则过渡可能看起来很困难。我最近提供的另一个答案启发了这段代码:https : //stackoverflow.com/a/8253729/717357
import numpy as np
import matplotlib.pyplot as plt
def highResPoints(x,y,factor=10):
'''
Take points listed in two vectors and return them at a higher
resultion. Create at least factor*len(x) new points that include the
original points and those spaced in between.
Returns new x and y arrays as a tuple (x,y).
'''
# r is the distance spanned between pairs of points
r = [0]
for i in range(1,len(x)):
dx = x[i]-x[i-1]
dy = y[i]-y[i-1]