Files
simulated-examination/q4-1.py
2026-07-05 18:42:26 +08:00

32 lines
794 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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