diff --git a/q4/q4_3b/q4_3b.py b/q4/q4_3b/q4_3b.py new file mode 100644 index 0000000..948a7be --- /dev/null +++ b/q4/q4_3b/q4_3b.py @@ -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() \ No newline at end of file