完成q4_1:电影类型柱状图,包含代码、数据和图片
This commit is contained in:
105
q4/q4_1/movies.json
Normal file
105
q4/q4_1/movies.json
Normal file
@@ -0,0 +1,105 @@
|
||||
{
|
||||
"data_code": "B-20260705-9162",
|
||||
"movies": [
|
||||
{
|
||||
"id": 1,
|
||||
"title": "放牛班的春天",
|
||||
"director": "Frank Darabont",
|
||||
"year": 2007,
|
||||
"rating": 8.9,
|
||||
"duration": 155,
|
||||
"genre": "悬疑",
|
||||
"actors_count": 5
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"title": "霸王别姬",
|
||||
"director": "陈凯歌",
|
||||
"year": 2018,
|
||||
"rating": 6.6,
|
||||
"duration": 143,
|
||||
"genre": "剧情",
|
||||
"actors_count": 4
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"title": "星际穿越",
|
||||
"director": "Robert Zemeckis",
|
||||
"year": 2018,
|
||||
"rating": 8.4,
|
||||
"duration": 165,
|
||||
"genre": "悬疑",
|
||||
"actors_count": 5
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"title": "肖申克的救赎",
|
||||
"director": "James Cameron",
|
||||
"year": 2008,
|
||||
"rating": 8.6,
|
||||
"duration": 124,
|
||||
"genre": "剧情",
|
||||
"actors_count": 5
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"title": "盗梦空间",
|
||||
"director": "宫崎骏",
|
||||
"year": 1993,
|
||||
"rating": 7.8,
|
||||
"duration": 90,
|
||||
"genre": "爱情",
|
||||
"actors_count": 2
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"title": "泰坦尼克号",
|
||||
"director": "Christopher Nolan",
|
||||
"year": 2001,
|
||||
"rating": 6.7,
|
||||
"duration": 175,
|
||||
"genre": "喜剧",
|
||||
"actors_count": 4
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"title": "忠犬八公的故事",
|
||||
"director": "Lasse Hallström",
|
||||
"year": 2004,
|
||||
"rating": 8.2,
|
||||
"duration": 91,
|
||||
"genre": "动画",
|
||||
"actors_count": 3
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"title": "三傻大闹宝莱坞",
|
||||
"director": "Rajkumar Hirani",
|
||||
"year": 2011,
|
||||
"rating": 6.3,
|
||||
"duration": 175,
|
||||
"genre": "冒险",
|
||||
"actors_count": 3
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"title": "阿甘正传",
|
||||
"director": "Christophe Barratier",
|
||||
"year": 2022,
|
||||
"rating": 7.9,
|
||||
"duration": 107,
|
||||
"genre": "动画",
|
||||
"actors_count": 4
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"title": "千与千寻",
|
||||
"director": "Christopher Nolan",
|
||||
"year": 1993,
|
||||
"rating": 7.2,
|
||||
"duration": 129,
|
||||
"genre": "悬疑",
|
||||
"actors_count": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
45
q4/q4_1/q4_1.py
Normal file
45
q4/q4_1/q4_1.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import json
|
||||
import matplotlib.pyplot as plt
|
||||
from collections import Counter
|
||||
|
||||
# 设置中文字体(解决中文乱码问题,必加!)
|
||||
plt.rcParams['font.sans-serif'] = ['SimHei'] # 黑体
|
||||
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
|
||||
|
||||
# 1. 读取movies.json数据
|
||||
try:
|
||||
with open('movies.json', 'r', encoding='utf-8') as f:
|
||||
movies_data = json.load(f)
|
||||
except FileNotFoundError:
|
||||
print("错误:未找到movies.json文件,请确认文件在当前目录!")
|
||||
exit()
|
||||
|
||||
# 2. 统计各类型电影数量(处理多类型分隔,比如"剧情/喜剧"拆分为两个类型)
|
||||
genre_list = []
|
||||
for movie in movies_data:
|
||||
# 假设movies.json里的类型字段是"genre",如果是其他名称(如"type")请修改
|
||||
genres = movie.get('genre', '').split('/') # 按/拆分多类型
|
||||
genre_list.extend([g.strip() for g in genres if g.strip()]) # 去空格并过滤空值
|
||||
|
||||
genre_counter = Counter(genre_list) # 统计各类型数量
|
||||
|
||||
# 3. 提取X轴(类型)和Y轴(数量)数据
|
||||
x_labels = list(genre_counter.keys())
|
||||
y_values = list(genre_counter.values())
|
||||
|
||||
# 4. 绘制柱状图
|
||||
plt.figure(figsize=(10, 6)) # 设置画布大小
|
||||
plt.bar(x_labels, y_values, color='skyblue') # 用plt.bar绘制柱状图
|
||||
|
||||
# 5. 设置标题和坐标轴(按要求配置)
|
||||
plt.title("类型电影数量分布", fontsize=14) # 标题
|
||||
plt.xlabel("电影类型", fontsize=12) # X轴标签(可选,更清晰)
|
||||
plt.ylabel("电影数量", fontsize=12) # Y轴标签(可选,更清晰)
|
||||
plt.xticks(rotation=45, ha='right') # X轴文字旋转,避免重叠(可选优化)
|
||||
plt.tight_layout() # 自动调整布局,防止文字被截断
|
||||
|
||||
# 6. 保存图片(按要求:q4_1_bar.png,dpi=150)
|
||||
plt.savefig('q4_1_bar.png', dpi=150)
|
||||
plt.close() # 关闭画布,释放资源
|
||||
|
||||
print("柱状图绘制完成!已保存为q4_1_bar.png")
|
||||
Reference in New Issue
Block a user