-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag.py
More file actions
84 lines (67 loc) · 2.86 KB
/
tag.py
File metadata and controls
84 lines (67 loc) · 2.86 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
76
77
78
79
80
81
82
83
84
import json
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.font_manager import FontProperties
# 使用 Agg backend
matplotlib.use('Agg')
# 设置自定义字体
custom_font = FontProperties(fname='zhuziA.ttf')
# 读取 JSON 文件
with open('top_50_tag_new.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 提取 name 和 count
names = [item['name'] for item in data]
counts = [item['count'] for item in data]
# 创建图表并设置图表大小
plt.figure(figsize=(16, 10)) # 可以调整图表的整体大小
# 创建柱状图
bars = plt.bar(range(len(names)), counts,
color='#39c5bb', # 设置柱子颜色,可以使用十六进制颜色码或颜色名称
width=0.7, # 设置柱子宽度(0-1之间,1表示无间距)
alpha=0.8, # 设置透明度(0-1之间)
edgecolor='#27ae60', # 设置边框颜色
linewidth=1) # 设置边框宽度
# 设置标题和标签
plt.title('ManyACG Top 50 Tags Distribution',
fontsize=20, # 设置标题字体大小
pad=20, # 设置标题与图表的距离
fontproperties=custom_font)
plt.xlabel('Tag Names',
fontsize=16, # 设置x轴标签字体大小
fontproperties=custom_font)
plt.ylabel('Count',
fontsize=16, # 设置y轴标签字体大小
fontproperties=custom_font)
# 设置 x 轴标签
plt.xticks(range(len(names)), names,
rotation=45, # 设置标签旋转角度
ha='right', # 水平对齐方式
fontsize=14, # 设置x轴刻度标签字体大小
fontproperties=custom_font)
# 设置 y 轴刻度标签字体大小
plt.yticks(fontsize=14)
# 在柱子顶部添加数值标签
for bar in bars:
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2., height,
f'{int(height)}',
ha='center', # 水平居中对齐
va='bottom', # 垂直对齐到底部
fontsize=8, # 设置数值标签字体大小
fontproperties=custom_font)
# 添加网格线
plt.grid(True,
axis='y', # 只显示水平网格线
linestyle='--', # 网格线样式
alpha=0.3, # 网格线透明度
color='gray') # 网格线颜色
# 调整布局
plt.tight_layout() # 自动调整布局,防止标签被切掉
# 设置图表边距
plt.subplots_adjust(bottom=0.2) # 增加底部边距,防止x轴标签被切掉
# 保存图片
plt.savefig('top_50_tags.png',
dpi=300, # 设置图片分辨率
bbox_inches='tight', # 自动调整边界
facecolor='white') # 设置背景颜色
plt.close()