回答編集履歴
2
Update
answer
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
```python
|
2
2
|
import numpy as np
|
3
|
+
from numpy.lib.recfunctions import append_fields
|
3
4
|
import matplotlib.pyplot as plt
|
4
5
|
|
5
6
|
# read names
|
@@ -10,24 +11,29 @@
|
|
10
11
|
|
11
12
|
# read data
|
12
13
|
csv_files = ['a.csv', 'b.csv', 'c.csv']
|
14
|
+
colors = ['r', 'b', 'g']
|
13
15
|
data_set = []
|
14
|
-
for fname in csv_files:
|
16
|
+
for i, fname in enumerate(csv_files):
|
15
|
-
data_set.append(
|
16
|
-
|
17
|
+
contents = np.genfromtxt(
|
17
|
-
|
18
|
+
fname=fname, names=True, dtype=None, delimiter=',')
|
19
|
+
contents = append_fields(
|
20
|
+
contents, 'name', [fname.split('.')[0]]*len(contents), usemask=False)
|
21
|
+
contents = append_fields(
|
22
|
+
contents, 'color', [colors[i]]*len(contents), usemask=False)
|
23
|
+
data_set.append(contents)
|
18
24
|
|
19
25
|
# plot data
|
20
26
|
for num, name in names:
|
21
|
-
data = np.concatenate(
|
22
|
-
|
27
|
+
data = np.concatenate([d[d['num'] == num] for d in data_set])
|
23
28
|
plt.figure(figsize=(8, 8))
|
29
|
+
for d in data:
|
24
|
-
|
30
|
+
plt.scatter(d['data1'], d['data2'], c=d['color'], label=d['name'])
|
25
31
|
plt.title(name); plt.xlabel(name); plt.ylabel(name)
|
26
32
|
plt.xlim(0,10); plt.ylim(0,10)
|
27
|
-
plt.grid()
|
33
|
+
plt.grid(); plt.legend()
|
28
34
|
plt.savefig(f'{num}_{name}.png')
|
29
35
|
```
|
30
36
|
|
31
37
|
※ `plt.savefig` で保存した 4 枚のプロット画像を `2行x2列` で結合
|
32
38
|
|
33
|
-

|
1
Update
answer
CHANGED
@@ -17,13 +17,9 @@
|
|
17
17
|
fname=fname, names=True, dtype=None, delimiter=','))
|
18
18
|
|
19
19
|
# plot data
|
20
|
-
|
20
|
+
for num, name in names:
|
21
|
-
|
21
|
+
data = np.concatenate(
|
22
|
-
|
22
|
+
[d[d['num'] == num][['data1', 'data2']] for d in data_set])
|
23
|
-
for num, name in names]
|
24
|
-
|
25
|
-
# plotting
|
26
|
-
for num, name, data in plot_data:
|
27
23
|
plt.figure(figsize=(8, 8))
|
28
24
|
plt.scatter(data['data1'], data['data2'])
|
29
25
|
plt.title(name); plt.xlabel(name); plt.ylabel(name)
|