fix color

This commit is contained in:
2026-01-19 19:38:38 +08:00
parent 22003ea65e
commit b98ba91fc1
20 changed files with 436 additions and 32 deletions

View File

@@ -19,6 +19,7 @@ Task 3 - Step 8: 可视化Fig.1/2/4/5
from __future__ import annotations
from dataclasses import dataclass
import json
from pathlib import Path
import os
@@ -38,11 +39,62 @@ import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
from cycler import cycler
BASE_DIR = Path(__file__).resolve().parent
MORANDI = {
"bg": "#F5F2ED",
"grid": "#D7D1C7",
"text": "#3F3F3F",
"muted_blue": "#8FA3A7",
"muted_blue_dark": "#6E858A",
"sage": "#AEB8A6",
"clay": "#C6AA9B",
"rose": "#D2B8B3",
"warm_gray": "#8C857A",
"terracotta": "#B07A6A",
}
def _apply_morandi_style() -> None:
plt.rcParams.update(
{
"figure.facecolor": MORANDI["bg"],
"axes.facecolor": MORANDI["bg"],
"savefig.facecolor": MORANDI["bg"],
"axes.edgecolor": MORANDI["grid"],
"axes.labelcolor": MORANDI["text"],
"xtick.color": MORANDI["text"],
"ytick.color": MORANDI["text"],
"text.color": MORANDI["text"],
"grid.color": MORANDI["grid"],
"grid.alpha": 0.55,
"axes.grid": True,
"axes.titleweight": "semibold",
"axes.titlesize": 11,
"axes.labelsize": 10,
"legend.frameon": True,
"legend.framealpha": 0.9,
"legend.facecolor": MORANDI["bg"],
"legend.edgecolor": MORANDI["grid"],
"axes.prop_cycle": cycler(
"color",
[
MORANDI["muted_blue"],
MORANDI["sage"],
MORANDI["clay"],
MORANDI["rose"],
MORANDI["warm_gray"],
],
),
}
)
@dataclass(frozen=True)
class Paths:
distance_xlsx: Path
@@ -76,6 +128,15 @@ def _pair_key(a: int, b: int) -> tuple[int, int]:
def fig1_pairing_map(paths: Paths) -> Path:
_apply_morandi_style()
# Warmer + higher contrast for Fig.1 specifically (clearer on paper/screens)
fig1_colors = {
"paired": "#e76f51",
"unpaired": "#6c757d",
"link": "#bc6c25",
}
sites = pd.read_excel(paths.distance_xlsx, sheet_name="sites")
pairs = pd.read_excel(paths.pairing_xlsx, sheet_name="selected_pairs")
@@ -97,9 +158,9 @@ def fig1_pairing_map(paths: Paths) -> Path:
ax.plot(
[si["lon"], sj["lon"]],
[si["lat"], sj["lat"]],
color="#2c7fb8",
alpha=0.35,
linewidth=1.0,
color=fig1_colors["link"],
alpha=0.5,
linewidth=1.2,
zorder=1,
)
@@ -110,8 +171,8 @@ def fig1_pairing_map(paths: Paths) -> Path:
paired["lon"],
paired["lat"],
s=18,
color="#d95f0e",
alpha=0.85,
color=fig1_colors["paired"],
alpha=0.9,
label=f"Paired sites (n={len(paired)})",
zorder=2,
)
@@ -120,7 +181,7 @@ def fig1_pairing_map(paths: Paths) -> Path:
unpaired["lon"],
unpaired["lat"],
s=22,
color="#636363",
color=fig1_colors["unpaired"],
alpha=0.9,
marker="x",
label=f"Unpaired sites (n={len(unpaired)})",
@@ -130,7 +191,7 @@ def fig1_pairing_map(paths: Paths) -> Path:
ax.set_title("Fig.1 Pairing map (sites + selected 34 links)")
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
ax.grid(True, alpha=0.2)
ax.grid(True, alpha=0.55)
ax.legend(loc="best", frameon=True)
out = paths.figures_dir / "fig1_pairing_map.png"
@@ -140,7 +201,58 @@ def fig1_pairing_map(paths: Paths) -> Path:
return out
def export_fig1_points_js(paths: Paths) -> Path:
sites = pd.read_excel(paths.distance_xlsx, sheet_name="sites").copy()
pairs = pd.read_excel(paths.pairing_xlsx, sheet_name="selected_pairs").copy()
sites["site_id"] = sites["site_id"].astype(int)
sites["k"] = sites["k"].astype(int)
paired_ids = set(pairs["site_i_id"].astype(int)).union(
set(pairs["site_j_id"].astype(int))
)
points = []
for _, r in sites.iterrows():
points.append(
{
"site_id": int(r["site_id"]),
"site_name": str(r["site_name"]),
"lat": float(r["lat"]),
"lng": float(r["lon"]),
"mu": float(r["mu"]),
"sigma": float(r["sigma"]),
"k": int(r["k"]),
"is_paired": int(r["site_id"]) in paired_ids,
}
)
links = []
for _, r in pairs.iterrows():
links.append(
{
"site_i_id": int(r["site_i_id"]),
"site_j_id": int(r["site_j_id"]),
"site_i_name": str(r["site_i_name"]),
"site_j_name": str(r["site_j_name"]),
"distance": float(r["distance"]),
}
)
out = BASE_DIR / "fig1_points.js"
payload = (
"// Auto-generated from `task3/01_distance.xlsx` (sites) + `task3/02_pairing.xlsx` (selected_pairs)\n"
"// Usage: open `task3/fig1_carto.html` in a browser (same directory must contain this file).\n"
f"window.FIG1_POINTS = {json.dumps(points, ensure_ascii=False, separators=(',', ':'))};\n"
f"window.FIG1_LINKS = {json.dumps(links, ensure_ascii=False, separators=(',', ':'))};\n"
)
out.write_text(payload, encoding="utf-8")
return out
def fig2_allocation_scatter(paths: Paths) -> Path:
_apply_morandi_style()
df = pd.read_excel(paths.allocation_xlsx, sheet_name="allocation")
q_ratio = df["q_ratio"].to_numpy(dtype=float)
@@ -157,15 +269,22 @@ def fig2_allocation_scatter(paths: Paths) -> Path:
]
for ax, (title, x) in zip(axes, panels):
ax.scatter(x, q_ratio, s=22, alpha=0.75, color="#3182bd", edgecolor="none")
ax.scatter(
x,
q_ratio,
s=22,
alpha=0.8,
color=MORANDI["muted_blue"],
edgecolor="none",
)
if np.isfinite(x).all() and np.isfinite(q_ratio).all() and len(x) >= 2:
coef = np.polyfit(x, q_ratio, 1)
xx = np.linspace(float(np.min(x)), float(np.max(x)), 100)
yy = coef[0] * xx + coef[1]
ax.plot(xx, yy, color="#de2d26", linewidth=2.0, alpha=0.9)
ax.plot(xx, yy, color=MORANDI["terracotta"], linewidth=2.0, alpha=0.95)
ax.set_title(title)
ax.set_xlabel("x")
ax.grid(True, alpha=0.2)
ax.grid(True, alpha=0.55)
axes[0].set_ylabel("$q^*/Q$")
fig.suptitle("Fig.2 Allocation strategy scatter (34 pairs)", y=1.02)
@@ -178,6 +297,8 @@ def fig2_allocation_scatter(paths: Paths) -> Path:
def fig4_calendar_heatmap(paths: Paths) -> Path:
_apply_morandi_style()
sites = pd.read_excel(paths.distance_xlsx, sheet_name="sites")[["site_id", "mu"]]
sites["site_id"] = sites["site_id"].astype(int)
mu_by_id = dict(zip(sites["site_id"], sites["mu"]))
@@ -212,7 +333,17 @@ def fig4_calendar_heatmap(paths: Paths) -> Path:
expected[day - 1, slot - 1] = pair_E.get(_pair_key(i_id, j_id), np.nan)
fig, ax = plt.subplots(figsize=(12, 2.6), dpi=200)
im = ax.imshow(expected.T, aspect="auto", cmap="viridis")
cmap = mcolors.LinearSegmentedColormap.from_list(
"morandi",
[
MORANDI["bg"],
"#E7E1D7",
MORANDI["sage"],
MORANDI["muted_blue"],
"#5F7478",
],
)
im = ax.imshow(expected.T, aspect="auto", cmap=cmap)
ax.set_yticks([0, 1], labels=["Slot 1", "Slot 2"])
ax.set_xlabel("Day of year")
ax.set_title("Fig.4 Calendar heatmap (expected service per slot)")
@@ -233,6 +364,8 @@ def fig4_calendar_heatmap(paths: Paths) -> Path:
def fig5_risk_distribution(paths: Paths) -> Path:
_apply_morandi_style()
risk = pd.read_excel(paths.evaluate_xlsx, sheet_name="pair_risk").copy()
p = risk["shortfall_prob_either"].to_numpy(dtype=float)
p = p[np.isfinite(p)]
@@ -240,21 +373,36 @@ def fig5_risk_distribution(paths: Paths) -> Path:
fig, axes = plt.subplots(1, 2, figsize=(11, 3.8), dpi=200)
ax = axes[0]
ax.hist(p, bins=10, color="#3182bd", alpha=0.85, edgecolor="white")
ax.axvline(float(np.mean(p)), color="#de2d26", linewidth=2.0, label=f"mean={np.mean(p):.3f}")
ax.hist(p, bins=10, color=MORANDI["sage"], alpha=0.9, edgecolor=MORANDI["bg"])
ax.axvline(
float(np.mean(p)),
color=MORANDI["terracotta"],
linewidth=2.2,
label=f"mean={np.mean(p):.3f}",
)
ax.set_title("Histogram")
ax.set_xlabel("Shortfall probability (either site)")
ax.set_ylabel("Count")
ax.grid(True, alpha=0.2)
ax.grid(True, alpha=0.55)
ax.legend(loc="best", frameon=True)
ax = axes[1]
p_sorted = np.sort(p)[::-1]
ax.plot(np.arange(1, len(p_sorted) + 1), p_sorted, marker="o", markersize=3.5, linewidth=1.5)
ax.plot(
np.arange(1, len(p_sorted) + 1),
p_sorted,
marker="o",
markersize=3.2,
linewidth=1.6,
color=MORANDI["muted_blue_dark"],
markerfacecolor=MORANDI["muted_blue"],
markeredgecolor=MORANDI["bg"],
markeredgewidth=0.6,
)
ax.set_title("Sorted by risk (descending)")
ax.set_xlabel("Pair rank")
ax.set_ylabel("Shortfall probability")
ax.grid(True, alpha=0.2)
ax.grid(True, alpha=0.55)
fig.suptitle("Fig.5 Risk distribution across 34 pairs", y=1.02)
fig.tight_layout()
@@ -282,6 +430,8 @@ def main() -> None:
out1 = fig1_pairing_map(paths)
print(f"Saved: {out1.relative_to(BASE_DIR)}")
out1_js = export_fig1_points_js(paths)
print(f"Saved: {out1_js.relative_to(BASE_DIR)}")
out2 = fig2_allocation_scatter(paths)
print(f"Saved: {out2.relative_to(BASE_DIR)}")
out4 = fig4_calendar_heatmap(paths)