Files
simulated-examination/q4/q4_1/q4_1.py
2026-07-06 14:27:34 +08:00

30 lines
645 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_1.py
import json
import matplotlib.pyplot as plt
from collections import Counter
# 读取上级目录的 movies.json
with open("../q4_2/movies.json", "r", encoding="utf-8") as f:
data = json.load(f)
movies = data["movies"]
# 统计每个类型有多少部电影
genres = [m["genre"] for m in movies]
genre_count = Counter(genres)
# X轴类型名称 Y轴数量
x = list(genre_count.keys())
y = list(genre_count.values())
# 绘制柱状图
plt.bar(x, y)
# 标题
plt.title("类型电影数量分布")
# 保存图片
plt.savefig("q4_1_bar.png", dpi=150)
plt.close()
print("✅ q4_1 柱状图完成!已生成 q4_1_bar.png")