LineCollection は複数の線分をまとめたオブジェクトで、コンストラクタの第一引数 segments には、線分の一覧をリスト [line1, line2, ...] で指定します。
各 line は [(x1, y1), (x2, y2), ...] とその線分を構成する点の一覧になります。
色は線分ごとに設定できるようになっているので、x = (0,1,2,3,4) y = (0,4,3,5,7) を各区間ごとに色分けしたい場合、以下の4つの線分を作成する必要があります。
lines = [[(0, 0), (1, 4)], # 線分1
[(1, 4), (2, 3)], # 線分2
[(2, 3), (3, 5)], # 線分3
[(3, 5), (4, 7)] # 線分4
]
サンプルコード
python
1import matplotlib.pyplot as plt
2import numpy as np
3from matplotlib.collections import LineCollection
4
5xs = [0, 1, 2, 3, 4]
6ys = [0, 4, 3, 5, 7]
7lines = [[(x1, y1), (x2, y2)] for x1, y1, x2, y2 in zip(xs, ys, xs[1:], ys[1:])]
8print(len(lines))
9colors = ["r", "g", "b", "y"]
10lc = LineCollection(lines, colors=colors)
11
12fig, ax = plt.subplots()
13ax.add_collection(lc)
14ax.autoscale()
15plt.show()

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。