回答編集履歴
2
修正
answer
CHANGED
@@ -14,15 +14,21 @@
|
|
14
14
|
## サンプルコード
|
15
15
|
|
16
16
|
```python
|
17
|
+
import matplotlib.pyplot as plt
|
18
|
+
import numpy as np
|
19
|
+
from matplotlib.collections import LineCollection
|
20
|
+
|
17
|
-
xs =
|
21
|
+
xs = [0, 1, 2, 3, 4]
|
18
|
-
ys =
|
22
|
+
ys = [0, 4, 3, 5, 7]
|
19
|
-
colors = ["r", "g", "b", "y"] # 4つの区間があるので4色
|
20
23
|
lines = [[(x1, y1), (x2, y2)] for x1, y1, x2, y2 in zip(xs, ys, xs[1:], ys[1:])]
|
24
|
+
print(len(lines))
|
25
|
+
colors = ["r", "g", "b", "y"]
|
21
26
|
lc = LineCollection(lines, colors=colors)
|
22
27
|
|
23
28
|
fig, ax = plt.subplots()
|
24
29
|
ax.add_collection(lc)
|
25
30
|
ax.autoscale()
|
31
|
+
plt.show()
|
26
32
|
```
|
27
33
|
|
28
34
|

|
1
修正
answer
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
[LineCollection](https://matplotlib.org/3.2.1/api/collections_api.html#matplotlib.collections.LineCollection) は複数の線分をまとめたオブジェクトで、コンストラクタの第一引数 segments には、線分の一覧を [line1, line2, ...]
|
1
|
+
[LineCollection](https://matplotlib.org/3.2.1/api/collections_api.html#matplotlib.collections.LineCollection) は複数の線分をまとめたオブジェクトで、コンストラクタの第一引数 segments には、線分の一覧をリスト [line1, line2, ...] で指定します。
|
2
|
-
各 line は [(x1, y1), (x2, y2), ...] と
|
2
|
+
各 line は [(x1, y1), (x2, y2), ...] とその線分を構成する点の一覧になります。
|
3
3
|
|
4
4
|
色は線分ごとに設定できるようになっているので、x = (0,1,2,3,4) y = (0,4,3,5,7) を各区間ごとに色分けしたい場合、以下の4つの線分を作成する必要があります。
|
5
5
|
|