1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import matplotlib.pyplot as plt import numpy as np
plt.rcParams["font.sans-serif"] = ["SimHei", "Microsoft YaHei"] plt.rcParams["axes.unicode_minus"] = False
months = ["1月", "2月", "3月", "4月", "5月", "6月"] sales = [120, 135, 148, 160, 155, 170] costs = [80, 90, 95, 100, 98, 105]
plt.figure(figsize=(10, 6)) plt.plot(months, sales, "b-o", label="销售额", linewidth=2) plt.plot(months, costs, "r--s", label="成本", linewidth=2) plt.title("月度数据", fontsize=16) plt.xlabel("月份") plt.ylabel("金额(万元)") plt.legend() plt.grid(True, alpha=0.3) plt.tight_layout() plt.savefig("line_chart.png", dpi=150) plt.close()
categories = ["技术部", "市场部", "人事部", "财务部"] values = [45, 30, 15, 10]
plt.figure(figsize=(8, 5)) bars = plt.bar(categories, values, color=["#4472C4", "#ED7D31", "#A5A5A5", "#FFC000"]) plt.title("各部门人数") plt.ylabel("人数") for bar, val in zip(bars, values): plt.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5, str(val), ha="center", fontsize=12) plt.tight_layout() plt.savefig("bar_chart.png", dpi=150) plt.close()
labels = ["Python", "JavaScript", "Java", "Go", "其他"] sizes = [35, 25, 20, 12, 8] explode = (0.05, 0, 0, 0, 0)
plt.figure(figsize=(8, 8)) plt.pie(sizes, explode=explode, labels=labels, autopct="%1.1f%%", shadow=True, startangle=90, colors=plt.cm.Set3.colors) plt.title("编程语言使用占比") plt.tight_layout() plt.savefig("pie_chart.png", dpi=150) plt.close()
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
axes[0,0].plot(range(10), [x**2 for x in range(10)]) axes[0,0].set_title("二次函数")
x = np.random.randn(100) y = np.random.randn(100) axes[0,1].scatter(x, y, alpha=0.5) axes[0,1].set_title("散点图")
data = np.random.normal(0, 1, 1000) axes[1,0].hist(data, bins=30, edgecolor="black", alpha=0.7) axes[1,0].set_title("正态分布")
matrix = np.random.rand(5, 5) im = axes[1,1].imshow(matrix, cmap="YlOrRd") plt.colorbar(im, ax=axes[1,1]) axes[1,1].set_title("热力图")
plt.tight_layout() plt.savefig("subplots.png", dpi=150) plt.close()
|