注释太长,因此只想确认这LineCollection比在行子段上进行for循环要快得多。LineCollection方法在我手中要快得多。# Setupx = np.linspace(0,4*np.pi,1000)y = np.sin(x)MAP = 'cubehelix'NPOINTS = len(x)我们将针对上面的LineCollection方法测试迭代绘图。%%timeit -n1 -r1# Using IPython notebook timing magicsfig = plt.figure()ax1 = fig.add_subplot(111) # regular resolution color mapcm = plt.get_cmap(MAP)for i in range(10): ax1.set_color_cycle([cm(1.*i/(NPOINTS-1)) for i in range(NPOINTS-1)]) for i in range(NPOINTS-1): plt.plot(x[i:i+2],y[i:i+2])1 loops, best of 1: 13.4 s per loop%%timeit -n1 -r1 fig = plt.figure()ax1 = fig.add_subplot(111) # regular resolution color mapfor i in range(10): colorline(x,y,cmap='cubehelix', linewidth=1)1 loops, best of 1: 532 ms per loop如果您想要平滑的渐变并且只有几个点,那么按照当前选择的答案提供的方法,对线进行向上采样以获得更好的颜色渐变仍然是一个好主意。