上传文件至 q4/q4_3b

This commit is contained in:
2026-07-06 14:19:10 +08:00
parent c88464b376
commit fe0ec43a19

35
q4/q4_3b/q4_3b.py Normal file
View File

@@ -0,0 +1,35 @@
# 全部导入放最上方
import matplotlib.pyplot as plt
import json
import os
duration = []
# 读取json文件
with open(r'C:\Users\34481\OneDrive\Desktop\作业\新建文件夹\simulated-examination\q4\q4_3b\movies.json', 'r', encoding='utf-8') as f:
data = json.load(f)
# 提取时长数据
for i in data:
duration.append(i["duration"])
# 创建画布
plt.figure(figsize=(12, 8))
# 修正hist参数分行语法绘图参数完全不变图形一致
plt.hist(
duration, # 数据
bins=10, # 分成几个柱子
color='#3498DB', # 颜色
edgecolor='white'# 柱子边框颜色
)
# 图表文字设置(完全保留原参数)
plt.title('时长分布', fontsize=14)
plt.xlabel('时长(分钟)', fontsize=13)
plt.grid(True, linestyle='--', alpha=0.5, axis='y')
# 保存图片逻辑不变
save_path = os.path.join(os.path.dirname(__file__), "q4_3b_hist.png")
plt.savefig(save_path, dpi=150, format='png')
plt.show()