fix color
This commit is contained in:
@@ -16,17 +16,66 @@ Task 3 - Step 7: 敏感性分析 (Refined)
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from scipy import stats
|
||||
import matplotlib.pyplot as plt
|
||||
import warnings
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
warnings.filterwarnings('ignore')
|
||||
|
||||
# 设置绘图风格
|
||||
plt.style.use('seaborn-v0_8-whitegrid')
|
||||
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS', 'SimHei', 'DejaVu Sans'] # 适配中文
|
||||
plt.rcParams['axes.unicode_minus'] = False
|
||||
# Matplotlib cache dirs(避免 home 目录不可写导致的警告)
|
||||
_mpl_config_dir = Path(tempfile.gettempdir()) / "mm_task3_mplconfig"
|
||||
_xdg_cache_dir = Path(tempfile.gettempdir()) / "mm_task3_xdg_cache"
|
||||
_mpl_config_dir.mkdir(parents=True, exist_ok=True)
|
||||
_xdg_cache_dir.mkdir(parents=True, exist_ok=True)
|
||||
os.environ.setdefault("MPLCONFIGDIR", str(_mpl_config_dir))
|
||||
os.environ.setdefault("XDG_CACHE_HOME", str(_xdg_cache_dir))
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
from cycler import cycler
|
||||
|
||||
# 设置绘图风格(低饱和度 Morandi 色系)
|
||||
MORANDI = {
|
||||
"bg": "#F5F2ED",
|
||||
"grid": "#D7D1C7",
|
||||
"text": "#3F3F3F",
|
||||
"muted_blue": "#8FA3A7",
|
||||
"muted_blue_dark": "#6E858A",
|
||||
"sage": "#AEB8A6",
|
||||
"terracotta": "#B07A6A",
|
||||
"warm_gray": "#8C857A",
|
||||
}
|
||||
|
||||
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.prop_cycle": cycler(
|
||||
"color",
|
||||
[
|
||||
MORANDI["muted_blue"],
|
||||
MORANDI["sage"],
|
||||
MORANDI["terracotta"],
|
||||
MORANDI["warm_gray"],
|
||||
],
|
||||
),
|
||||
"legend.frameon": True,
|
||||
"legend.framealpha": 0.9,
|
||||
"legend.facecolor": MORANDI["bg"],
|
||||
"legend.edgecolor": MORANDI["grid"],
|
||||
"font.sans-serif": ["Arial Unicode MS", "SimHei", "DejaVu Sans"],
|
||||
"axes.unicode_minus": False,
|
||||
}
|
||||
)
|
||||
|
||||
# ============================================
|
||||
# 基础参数和函数 (保持不变)
|
||||
@@ -333,16 +382,38 @@ for i, param in enumerate(params_list):
|
||||
data = df_results[df_results['param'] == param].sort_values('value')
|
||||
|
||||
# 绘制 E1/E2 - 左轴
|
||||
line1, = ax.plot(data['value'], data['E1'], 'b-', linewidth=1.6, label='Expected Service (E1)')
|
||||
line2, = ax.plot(data['value'], data['E2'], 'g-', linewidth=1.6, alpha=0.9, label='Quality-weighted (E2)')
|
||||
line1, = ax.plot(
|
||||
data['value'],
|
||||
data['E1'],
|
||||
linestyle='-',
|
||||
color=MORANDI["muted_blue_dark"],
|
||||
linewidth=1.8,
|
||||
label='Expected Service (E1)',
|
||||
)
|
||||
line2, = ax.plot(
|
||||
data['value'],
|
||||
data['E2'],
|
||||
linestyle='-',
|
||||
color=MORANDI["sage"],
|
||||
linewidth=1.8,
|
||||
alpha=0.95,
|
||||
label='Quality-weighted (E2)',
|
||||
)
|
||||
ax.set_xlabel(param_labels[param][0])
|
||||
ax.set_ylabel('Service (E1/E2)')
|
||||
|
||||
# 绘制 R1 (风险) - 右轴
|
||||
ax2 = ax.twinx()
|
||||
line3, = ax2.plot(data['value'], data['R1'], 'r--', linewidth=1.6, label='Shortfall Risk (R1)')
|
||||
ax2.set_ylabel('Risk Probability (R1)', color='r')
|
||||
ax2.tick_params(axis='y', labelcolor='r')
|
||||
line3, = ax2.plot(
|
||||
data['value'],
|
||||
data['R1'],
|
||||
linestyle='--',
|
||||
color=MORANDI["terracotta"],
|
||||
linewidth=1.8,
|
||||
label='Shortfall Risk (R1)',
|
||||
)
|
||||
ax2.set_ylabel('Risk Probability (R1)', color=MORANDI["terracotta"])
|
||||
ax2.tick_params(axis='y', labelcolor=MORANDI["terracotta"])
|
||||
|
||||
# 添加基准线
|
||||
if param == 'merge_ratio':
|
||||
@@ -354,7 +425,7 @@ for i, param in enumerate(params_list):
|
||||
elif param == 'cv_max':
|
||||
base_val = BASE_CV_MAX
|
||||
|
||||
ax.axvline(x=base_val, color='gray', linestyle=':', alpha=0.5)
|
||||
ax.axvline(x=base_val, color=MORANDI["warm_gray"], linestyle=':', alpha=0.65)
|
||||
|
||||
# 图例
|
||||
lines = [line1, line2, line3]
|
||||
@@ -362,7 +433,7 @@ for i, param in enumerate(params_list):
|
||||
ax.legend(lines, labels, loc='best', frameon=True)
|
||||
|
||||
ax.set_title(f'Effect of {param_labels[param][0]}')
|
||||
ax.grid(True, alpha=0.3)
|
||||
ax.grid(True, alpha=0.55)
|
||||
|
||||
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
|
||||
plt.savefig(OUTPUT_FIG, dpi=300)
|
||||
|
||||
Reference in New Issue
Block a user