-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3dbar_plot.py
executable file
·98 lines (82 loc) · 2.59 KB
/
3dbar_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
import argparse
import numpy as np
import matplotlib.pyplot as plt
from collections import defaultdict
from config import *
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--data-path",
required=True,
type=str,
help="Data file path.",
)
parser.add_argument("--fig-size", nargs="+", type=int, default=(5, 2))
args = parser.parse_args()
color = [
COLOR["red"],
COLOR["blue"],
COLOR["purple"],
COLOR["yellow"],
COLOR["green"],
]
hatch = [
HATCH["left"],
HATCH["right"],
HATCH["dot"],
]
sub_cat_repeat, sub_cat_length, sub_cat, sub_cat_data_group = 0, 0, [], []
cat = set([])
repeat_idx = 0
# collect data
with open(args.data_path) as f:
for i, line in enumerate(f.read().splitlines()):
if i == 0:
sub_cat = line.split(",")
sub_cat_length = len(sub_cat) - 1
sub_cat_repeat = int(sub_cat[-1])
sub_cat_data_group = [
[[] for _ in range(sub_cat_length)] for _ in range(sub_cat_repeat)
]
else:
for k, d in enumerate(line.split(",")):
if k < sub_cat_length:
sub_cat_data_group[repeat_idx][k].append(float(d))
else:
cat.add(d)
repeat_idx = (repeat_idx + 1) % sub_cat_repeat
# figure creation
fig, ax = plt.subplots(figsize=args.fig_size)
for i in range(sub_cat_length):
bottom = np.zeros(len(cat))
for k in range(sub_cat_repeat):
x = (
np.arange(len(cat)) * (sub_cat_length * BAR_WIDTH + BAR_GAP)
+ i * BAR_WIDTH
)
height = sub_cat_data_group[k][i]
if k == 0:
ax.bar(x, height, color=color[i], bottom=bottom, label=sub_cat[i])
else:
ax.bar(x, height, color=color[i], bottom=bottom, hatch=hatch[k - 1])
bottom += np.array(height)
xtick_list = (
np.arange(len(cat)) * (sub_cat_length * BAR_WIDTH + BAR_GAP)
+ sub_cat_length * BAR_WIDTH / 2
- BAR_WIDTH / 2
)
xtick_label_list = cat
ax.set_xticks(xtick_list)
ax.set_xticklabels(xtick_label_list)
plt.legend(
ncol=sub_cat_length,
frameon=False,
fancybox=False,
bbox_to_anchor=(0.5, 1.1),
loc="center",
)
plt.tight_layout()
plt.savefig("3dbar.png")
if __name__ == "__main__":
main()