且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python/matplotlib-多色线

更新时间:2023-11-09 20:10:52

我认为您不能在plot中使用颜色数组(文档说,颜色可以是任何matlab颜色,而文档说您可以使用数组).

I don't think that you can use an array of colors in plot (the documentation says that color can be any matlab color, while the scatter docs say you can use an array).

但是,您可以通过分别绘制每条线来伪造它:

However, you could fake it by plotting each line separately:

import numpy
from matplotlib import pyplot as plt

x = range(10)
y = numpy.random.choice(10,10)
for x1, x2, y1,y2 in zip(x, x[1:], y, y[1:]):
    if y1 > y2:
        plt.plot([x1, x2], [y1, y2], 'r')
    elif y1 < y2:
        plt.plot([x1, x2], [y1, y2], 'g')
    else:
        plt.plot([x1, x2], [y1, y2], 'b')

plt.show()