完成 q4_2 散点图
This commit is contained in:
105
q4/q4_2/movies.json
Normal file
105
q4/q4_2/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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
33
q4/q4_2/q4_2.py
Normal file
33
q4/q4_2/q4_2.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# q4_2.py
|
||||||
|
import json
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# 1. 读取文件
|
||||||
|
with open("movies.json", "r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
# 2. 安全提取电影列表
|
||||||
|
if type(data) is dict and "movies" in data:
|
||||||
|
movies = data["movies"]
|
||||||
|
else:
|
||||||
|
movies = []
|
||||||
|
|
||||||
|
# 3. 准备数据
|
||||||
|
x_data = []
|
||||||
|
y_data = []
|
||||||
|
|
||||||
|
for item in movies:
|
||||||
|
# 这一行彻底防止报错
|
||||||
|
if type(item) is dict:
|
||||||
|
x_data.append(item["duration"])
|
||||||
|
y_data.append(item["rating"])
|
||||||
|
|
||||||
|
# 4. 画图(题目全部要求)
|
||||||
|
plt.scatter(x_data, y_data, color="red", alpha=0.6)
|
||||||
|
plt.title("时长与评分关系散点图")
|
||||||
|
plt.xlabel("时长(分钟)")
|
||||||
|
plt.ylabel("评分")
|
||||||
|
plt.savefig("q4_2_scatter.png", dpi=150)
|
||||||
|
plt.close()
|
||||||
|
|
||||||
|
print("运行成功!")
|
||||||
BIN
q4/q4_2/q4_2_scatter.png
Normal file
BIN
q4/q4_2/q4_2_scatter.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
Reference in New Issue
Block a user