From 66e70b18d2d82aebac6ef6a270d95dce8faefb50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=A4=A9=E8=B5=90?= <2509165007@student.example.com> Date: Mon, 6 Jul 2026 17:06:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 41.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 41.py diff --git a/41.py b/41.py new file mode 100644 index 0000000..d2497bc --- /dev/null +++ b/41.py @@ -0,0 +1,41 @@ +# q4_1.py +import json +import matplotlib.pyplot as plt + +# 1. 读取books.json文件 +with open("books.json", "r", encoding="utf-8") as f: + data = json.load(f) +books = data["books"] + +# 2. 统计各出版社图书数量 +pub_count = {} +for book in books: + pub_name = book["publisher"] + if pub_name in pub_count: + pub_count[pub_name] += 1 + else: + pub_count[pub_name] = 1 + +# 拆分x轴、y轴数据 +publish_names = list(pub_count.keys()) +book_nums = list(pub_count.values()) + +# 3. 配置画布与绘图 +plt.figure(figsize=(10, 6)) +# 生成不同颜色 +colors = plt.cm.tab10(range(len(publishing_names))) +# 绘制柱状图 +plt.bar(publishing_names, book_nums, color=colors) + +# 4. 图表样式设置 +plt.title("出版社图书数量分布", fontsize=14) +plt.xlabel("出版社名称", fontsize=12) +plt.ylabel("图书数量", fontsize=12) +# X轴文字旋转45度防止重叠 +plt.xticks(rotation=45, ha="right") +# 自动调整布局,避免标签被截断 +plt.tight_layout() + +# 5. 保存图片 +plt.savefig("q4_1_bar.png", dpi=150) +plt.show() \ No newline at end of file