From ebe20653bd3055bb6066fffd03547c17a7de80d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E5=AE=87=E6=B6=B5?= <2509165016@student.example.com> Date: Sun, 5 Jul 2026 18:35:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20q4-1.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- q4-1.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 q4-1.py diff --git a/q4-1.py b/q4-1.py new file mode 100644 index 0000000..dbd889b --- /dev/null +++ b/q4-1.py @@ -0,0 +1,28 @@ +# 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() \ No newline at end of file