Files
2026-07-06 14:24:41 +08:00

35 lines
1001 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 全部导入放最上方
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()