-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_response.py
More file actions
executable file
·66 lines (51 loc) · 2.14 KB
/
plot_response.py
File metadata and controls
executable file
·66 lines (51 loc) · 2.14 KB
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
#!/usr/bin/env python3
import argparse
import json
import re
from pathlib import Path
import matplotlib.pyplot as plt
def load_series(path: Path) -> list[float]:
text = path.read_text(encoding="utf-8")
# First try strict JSON.
try:
data = json.loads(text)
if not isinstance(data, list):
raise ValueError("data.json 顶层不是数组")
return [float(x) for x in data]
except Exception:
# Fallback for relaxed formats such as trailing comma.
nums = re.findall(r"[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?", text)
if not nums:
raise ValueError("未从 data.json 读取到任何数值")
return [float(x) for x in nums]
def plot_response(values: list[float], out_path: Path, show: bool, sea_level: float) -> None:
x = list(range(len(values)))
plt.figure(figsize=(10, 4.8), dpi=120)
plt.plot(x, values, linewidth=1.6, color="#1f77b4", label="Response")
plt.axhline(y=sea_level, color="#2ca02c", linestyle=":", linewidth=1.2, label=f"Sea Level ({sea_level:g})")
plt.title("Response Curve")
plt.xlabel("Sample Index")
plt.ylabel("Value")
plt.ylim(bottom=sea_level)
plt.grid(True, linestyle="--", alpha=0.35)
plt.legend()
plt.tight_layout()
plt.savefig(out_path, bbox_inches="tight")
print(f"图像已保存: {out_path}")
if show:
plt.show()
else:
plt.close()
def main() -> None:
parser = argparse.ArgumentParser(description="根据 data.json 绘制响应曲线")
parser.add_argument("-i", "--input", default="data.json", help="输入数据文件路径")
parser.add_argument("-o", "--output", default="response_curve.png", help="输出图像路径")
parser.add_argument("--sea-level", type=float, default=63.0, help="MC 海平面高度(默认 63)")
parser.add_argument("--show", action="store_true", help="绘图后弹窗显示")
args = parser.parse_args()
values = load_series(Path(args.input))
if len(values) == 0:
raise ValueError("数据为空,无法绘图")
plot_response(values, Path(args.output), args.show, args.sea_level)
if __name__ == "__main__":
main()