from pathlib import Path from typing import List, Tuple import matplotlib.pyplot as plt def read_points(filename: str) -> List[Tuple[float, float, float]]: pts = [] for line in Path(filename).read_text(encoding="utf-8").splitlines(): line = line.strip() if not line or line.startswith("#"): continue parts = line.split() if len(parts) < 3: continue x = float(parts[0]) y = float(parts[1]) phi = float(parts[2]) pts.append((x, y, phi)) return pts def phi_vs_x_at_y( points: List[Tuple[float, float, float]], y_target: float, tol: float = 1e-6 ): xs = [] phis = [] for x, y, phi in points: if abs(y - y_target) <= tol: xs.append(x) phis.append(phi) paired = sorted(zip(xs, phis), key=lambda p: p[0]) if not paired: return [], [] xs_sorted, phis_sorted = zip(*paired) return list(xs_sorted), list(phis_sorted) p1 = read_points("points1.txt") p2 = read_points("points2.txt") x1, yphi1 = phi_vs_x_at_y(p1, 10.0) x2, yphi2 = phi_vs_x_at_y(p2, 10.0) plt.figure(figsize=(10, 5)) if x1: plt.plot(x1, yphi1, marker="o", linestyle="-", label="points1.txt (Y=10)") if x2: plt.plot(x2, yphi2, marker="s", linestyle="--", label="points2.txt (Y=10)") plt.xlabel("X (см)") plt.ylabel("φ (В)") plt.title("Зависимость φ = φ(X) при Y = 10 см") plt.grid(alpha=0.4) plt.legend() plt.savefig("phi_vs_x_Y10.png", dpi=300)