AREA PLOT
绘图 · 第 8 / 36 章
54 字约 1 分钟
基础面积图

基础面积图
Python26 行
import matplotlib.pyplot as plt
import numpy as np
COLORS = ["#A8552E", "#35666E", "#D0A64F", "#3F2E18", "#7E8F52"]
np.random.seed(42)
x = np.arange(1, 25)
y = 50 + 5 * np.sin(x / 3) + np.random.normal(0, 2, len(x))
fig, ax = plt.subplots(figsize=(9, 5))
ax.fill_between(x, y, color=COLORS[0], alpha=0.35, zorder=1)
ax.plot(x, y, color=COLORS[0], linewidth=2.2, zorder=2)
ax.set_title("Area Plot — Single Series", fontsize=15, color=COLORS[3], pad=12)
ax.set_xlabel("Time", fontsize=12, color=COLORS[3])
ax.set_ylabel("Value", fontsize=12, color=COLORS[3])
ax.tick_params(axis="both", colors=COLORS[3])
for s in ("top", "right"):
ax.spines[s].set_visible(False)
ax.spines["left"].set_color(COLORS[3])
ax.spines["bottom"].set_color(COLORS[3])
plt.tight_layout()
plt.show()堆叠面积图

堆叠面积图
Python41 行
import matplotlib.pyplot as plt
import numpy as np
COLORS = ["#A8552E", "#35666E", "#D0A64F", "#3F2E18", "#7E8F52"]
np.random.seed(0)
x = np.arange(1, 25)
series = {
"Series A": 20 + 5 * np.sin(x / 4) + np.random.normal(0, 1.5, len(x)),
"Series B": 15 + 4 * np.cos(x / 5) + np.random.normal(0, 1.5, len(x)),
"Series C": 12 + 3 * np.sin(x / 3 + 1) + np.random.normal(0, 1.5, len(x)),
"Series D": 10 + 2 * np.cos(x / 6) + np.random.normal(0, 1.5, len(x)),
}
fig, ax = plt.subplots(figsize=(9.5, 5.5))
ax.stackplot(
x, *series.values(),
labels=list(series.keys()),
colors=COLORS[:len(series)],
alpha=0.85,
edgecolor="white",
linewidth=0.8,
)
ax.set_title("Stacked Area Plot", fontsize=15, color=COLORS[3], pad=12)
ax.set_xlabel("Time", fontsize=12, color=COLORS[3])
ax.set_ylabel("Cumulative Value", fontsize=12, color=COLORS[3])
ax.tick_params(axis="both", colors=COLORS[3])
# 图例放右上,横排两列
ax.legend(frameon=False, fontsize=10, labelcolor=COLORS[3],
loc="upper right", ncol=2)
for s in ("top", "right"):
ax.spines[s].set_visible(False)
ax.spines["left"].set_color(COLORS[3])
ax.spines["bottom"].set_color(COLORS[3])
plt.tight_layout()
plt.show()百分比堆叠面积图

百分比堆叠面积图
Python56 行
import matplotlib.pyplot as plt
import numpy as np
COLORS = ["#A8552E", "#35666E", "#D0A64F", "#3F2E18", "#7E8F52"]
np.random.seed(1)
x = np.arange(1, 25)
series = {
"Series A": 20 + 5 * np.sin(x / 4) + np.random.normal(0, 1, len(x)),
"Series B": 15 + 4 * np.cos(x / 5) + np.random.normal(0, 1, len(x)),
"Series C": 12 + 3 * np.sin(x / 3 + 1) + np.random.normal(0, 1, len(x)),
}
data = np.array([np.abs(v) for v in series.values()])
data_pct = data / data.sum(axis=0) * 100
fig, ax = plt.subplots(figsize=(9.5, 5.5))
ax.stackplot(
x, *data_pct,
labels=list(series.keys()),
colors=COLORS[:len(series)],
alpha=0.85,
edgecolor="white",
linewidth=0.8,
)
ax.set_ylim(0, 100)
ax.set_yticks([0, 25, 50, 75, 100])
ax.set_yticklabels(["0%", "25%", "50%", "75%", "100%"])
# ---- 标题上移,给图例留出空间 ----
ax.set_title(
"100% Stacked Area Plot",
fontsize=15, color=COLORS[3],
pad=38, # 原来 12,加大到 38 把标题顶上去
)
ax.set_xlabel("Time", fontsize=12, color=COLORS[3])
ax.set_ylabel("Share", fontsize=12, color=COLORS[3])
ax.tick_params(axis="both", colors=COLORS[3])
# ---- 图例紧贴坐标轴上方,位于标题下方 ----
ax.legend(
frameon=False, fontsize=10, labelcolor=COLORS[3],
loc="lower left",
bbox_to_anchor=(0, 1.02),
ncol=3,
)
for s in ("top", "right"):
ax.spines[s].set_visible(False)
ax.spines["left"].set_color(COLORS[3])
ax.spines["bottom"].set_color(COLORS[3])
plt.tight_layout()
plt.show()面积图 + 基线

面积图 + 基线
Python45 行
import matplotlib.pyplot as plt
import numpy as np
COLORS = ["#A8552E", "#35666E", "#D0A64F", "#3F2E18", "#7E8F52"]
# ---- 新的数据:同时跨越基准线上下 ----
np.random.seed(7)
x = np.arange(1, 31)
# 正弦趋势 + 缓慢上升的漂移 + 噪声
y = (6 * np.sin(x / 5) # 主要波动,上下穿越 0
+ 0.35 * x # 缓慢上升的趋势
- 5) # 让起点在 0 附近
y = y + np.random.normal(0, 1.2, len(x)) # 叠加噪声
y = y - y[0] # 强制起点为 0,便于视觉对照
baseline = 0
# ---------------------------------------
fig, ax = plt.subplots(figsize=(9.5, 5.5))
# 高于基准线 → 主色
ax.fill_between(x, y, baseline, where=(y >= baseline),
color=COLORS[0], alpha=0.45,
interpolate=True, label="Above baseline", zorder=1)
# 低于基准线 → 深青
ax.fill_between(x, y, baseline, where=(y < baseline),
color=COLORS[1], alpha=0.45,
interpolate=True, label="Below baseline", zorder=1)
ax.plot(x, y, color=COLORS[3], linewidth=2.0, zorder=2)
ax.axhline(baseline, color=COLORS[3], linewidth=1.0,
linestyle="--", alpha=0.6, zorder=2)
ax.set_title("Area Plot with Baseline", fontsize=15, color=COLORS[3], pad=12)
ax.set_xlabel("Time", fontsize=12, color=COLORS[3])
ax.set_ylabel("Value", fontsize=12, color=COLORS[3])
ax.tick_params(axis="both", colors=COLORS[3])
ax.legend(frameon=False, fontsize=10, labelcolor=COLORS[3], loc="upper left")
for s in ("top", "right"):
ax.spines[s].set_visible(False)
ax.spines["left"].set_color(COLORS[3])
ax.spines["bottom"].set_color(COLORS[3])
plt.tight_layout()
plt.show()