32 lines
794 B
Python
32 lines
794 B
Python
# q4/q4_1/q4_1.py
|
||
import json
|
||
import matplotlib.pyplot as plt
|
||
|
||
# 读取电影数据
|
||
with open("../q2_1_crawler/movies.json", "r", encoding="utf-8") as f:
|
||
data = json.load(f)
|
||
movies = data["movies"]
|
||
|
||
# 统计类型数量
|
||
genre_dict = {}
|
||
for m in movies:
|
||
g = m["genre"]
|
||
genre_dict[g] = genre_dict.get(g, 0) + 1
|
||
|
||
x = list(genre_dict.keys())
|
||
y = list(genre_dict.values())
|
||
|
||
# 绘制柱状图
|
||
plt.bar(x, y)
|
||
plt.title("类型电影数量分布")
|
||
plt.xlabel("电影类型")
|
||
plt.ylabel("电影数量")
|
||
plt.tight_layout() # 防止文字重叠
|
||
|
||
# 保存图片 dpi=150
|
||
plt.savefig("q4_1_bar.png", dpi=150)
|
||
plt.show()
|
||
|
||
打开 VS Code,顶部菜单栏点击 终端 (Terminal) → 新建终端 (New Terminal)
|
||
在底部弹出的终端窗口输入安装命令,回车执行
|
||
pip install matplotlib |