fix: 优化 README 内容问题,优化绘图

This commit is contained in:
2025-12-18 11:21:26 +08:00
parent 1c866d8fde
commit fbcb45c81b
6 changed files with 91 additions and 26 deletions

View File

@@ -16,16 +16,70 @@ FONT = font_manager.FontProperties(fname=config.FONT_PATH)
plt.rcParams["axes.unicode_minus"] = False
def plot_line(times, values, title, ylabel, out_path):
plt.figure(figsize=(8, 4))
plt.plot(times, values, marker="o")
def plot_line(
times,
normal_values,
title,
ylabel,
out_path,
free_values=None, # ✅ 免费额度可选
):
has_free = free_values is not None and len(free_values) > 0
plt.title(title, fontproperties=FONT)
plt.xlabel("时间", fontproperties=FONT)
plt.ylabel(ylabel, fontproperties=FONT)
if has_free:
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6), sharex=True)
else:
fig, ax1 = plt.subplots(1, 1, figsize=(8, 4))
ax2 = None
# ===== 普通额度 =====
ax1.plot(times, normal_values, marker="o")
ax1.set_title(
f"{title}(普通额度)" if has_free else title,
fontproperties=FONT,
)
ax1.set_ylabel(ylabel, fontproperties=FONT)
ax1.grid(True)
for i, (x, y) in enumerate(zip(times, normal_values)):
dy = 8 if i == 0 or y >= normal_values[i - 1] else -12
ax1.annotate(
f"{y}",
(x, y),
textcoords="offset points",
xytext=(0, dy),
ha="center",
fontsize=10,
fontproperties=FONT,
)
# ===== 免费额度(可选) =====
if has_free and ax2:
ax2.plot(times, free_values, marker="o", linestyle="--")
ax2.set_title(f"{title}(免费额度)", fontproperties=FONT)
ax2.set_xlabel("时间", fontproperties=FONT)
ax2.set_ylabel(ylabel, fontproperties=FONT)
ax2.grid(True)
for i, (x, y) in enumerate(zip(times, free_values)):
# 免费额度同样根据趋势决定标注方向
if i == 0:
dy = -8
else:
dy = -8 if y <= free_values[i - 1] else 8
ax2.annotate(
f"{y}",
(x, y),
textcoords="offset points",
xytext=(0, dy),
ha="center",
fontsize=8,
fontproperties=FONT,
)
plt.xticks(rotation=45, fontproperties=FONT)
plt.grid(True)
plt.tight_layout()
plt.savefig(out_path, dpi=150)
plt.close()