# 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")