diff --git a/q4_1.py b/q4_1.py new file mode 100644 index 0000000..67a3349 --- /dev/null +++ b/q4_1.py @@ -0,0 +1,26 @@ + +import json +import matplotlib.pyplot as plt + +with open("books.json", "r", encoding="utf-8") as f: + data = json.load(f) +books = data["books"] +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 +publish_names = list(pub_count.keys()) +book_nums = list(pub_count.values()) +plt.figure(figsize=(10, 6)) +plt.bar(pub_count.keys(), pub_count.values()) +plt.title("出版社图书数量分布", fontsize=14) +plt.xlabel("出版社名称", fontsize=12) +plt.ylabel("图书数量", fontsize=12) +plt.xticks(rotation=45, ha="right") +plt.tight_layout() + +plt.savefig("q4_1_bar.png", dpi=150) +plt.show() \ No newline at end of file diff --git a/q4_1_bar.png b/q4_1_bar.png new file mode 100644 index 0000000..cd0f853 Binary files /dev/null and b/q4_1_bar.png differ