-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
75 lines (67 loc) · 2.12 KB
/
plot.py
File metadata and controls
75 lines (67 loc) · 2.12 KB
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
from itertools import cycle
from json import load
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
with open('bgm_anime_dataset.json', 'r', encoding='utf8') as f:
data = load(f)
scores = np.array(
[bangumi['rating']['score'] for bangumi in data],
dtype=np.float64
)
count = scores.size
mean = np.mean(scores, dtype=np.float64)
median = np.median(scores)
min_score = np.min(scores)
max_score = np.max(scores)
n, bins = np.histogram(scores, bins=np.arange(-0.05, 10.05, 0.1))
hist = np.vstack((bins[:-1] + 0.05, n))
for i in range(hist.shape[1]):
print('%.1f %3d' % (hist[0, i], hist[1, i]))
rc('font', family='Sarasa Gothic SC', size=14)
fig, ax = plt.subplots()
for i, c in zip(range(20), cycle(('#fca2ae', '#f6c2d0'))):
ax.bar(hist[0, i*5:i*5+5], hist[1, i*5:i*5+5], width=0.05, color=c)
ax.annotate(
'最高:%d @ %.1f' % (hist[1, 66], hist[0, 66]),
(hist[0, 66], hist[1, 66]),
xycoords='data', xytext=(0.25, 0.56), textcoords='axes fraction',
arrowprops={'arrowstyle': '->'}
)
ax.annotate(
'局部最低:%d @ %.1f' % (hist[1, 69], hist[0, 69]),
(hist[0, 69], hist[1, 69]),
xycoords='data', xytext=(0.25, 0.43), textcoords='axes fraction',
arrowprops={'arrowstyle': '->'}
)
ax.annotate(
'平均:%.3f' % (mean),
(mean, 0),
xycoords='data', xytext=(0.58, -0.12), textcoords='axes fraction',
arrowprops={'arrowstyle': '->'}
)
ax.annotate(
'中位:%.1f' % (median),
(median, 0),
xycoords='data', xytext=(0.68, -0.12), textcoords='axes fraction',
arrowprops={'arrowstyle': '->'}
)
ax.annotate(
'最低:%.1f' % (min_score),
(min_score, 0),
xycoords='data', xytext=(0.125, -0.12), textcoords='axes fraction',
arrowprops={'arrowstyle': '->'}
)
ax.annotate(
'最高:%.1f' % (max_score),
(max_score, 0),
xycoords='data', xytext=(0.87, -0.12), textcoords='axes fraction',
arrowprops={'arrowstyle': '->'}
)
ax.grid(True, axis='y')
ax.set_xticks(np.arange(0, 10 + 0.1, 0.5))
ax.set_xlabel('评分')
ax.set_ylabel('动画数')
ax.set_title(f'Bangumi 动画评分分布 总计{count}部')
fig.tight_layout()
plt.show()