import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
# Hide the interactive navigation toolbar (home/pan/zoom/save) in the plot window.
mpl.rcParams["toolbar"] = "None"
def _hide_toolbar(fig) -> None:
mgr = getattr(fig.canvas, "manager", None)
tb = getattr(mgr, "toolbar", None)
if tb is None:
return
for m in ("setVisible", "hide", "pack_forget"):
fn = getattr(tb, m, None)
if callable(fn):
try:
fn(False) if m == "setVisible" else fn()
except Exception:
pass
def main() -> None:
# عدّل هذا المسار فقط إذا كان اسم ملفك مختلف.
path = r"C:\Users\pc\OneDrive\Desktop\Intro Ai ii.xlsx"
# Sheet 2 -> index 1. Columns A (Voltage) and B (Temperature).
df = pd.read_excel(path, sheet_name=1, usecols="A:B").dropna(how="all")
if {"Voltage", "Temperature"}.issubset(set(df.columns)):
x_raw = df["Voltage"]
y_raw = df["Temperature"]
else:
x_raw = df.iloc[:, 0]
y_raw = df.iloc[:, 1]
x_num = pd.to_numeric(x_raw, errors="coerce")
y_num = pd.to_numeric(y_raw, errors="coerce")
mask = x_num.notna() & y_num.notna()
X = x_num[mask].to_numpy(dtype=float).reshape(-1, 1)
y = y_num[mask].to_numpy(dtype=float)
sc_X = StandardScaler()
sc_y = StandardScaler()
X_scaled = sc_X.fit_transform(X)
y_scaled = sc_y.fit_transform(y.reshape(-1, 1)).ravel()
regressor = SVR(kernel="rbf")
regressor.fit(X_scaled, y_scaled)
x_plot = X.ravel()
plt.scatter(x_plot, y, color="red")
X_grid = np.linspace(x_plot.min(), x_plot.max(), 500).reshape(-1, 1)
y_grid_scaled = regressor.predict(sc_X.transform(X_grid))
y_grid = sc_y.inverse_transform(y_grid_scaled.reshape(-1, 1)).ravel()
plt.plot(X_grid.ravel(), y_grid, color="blue")
plt.title("Temperature Calibration (SVR)")
plt.xlabel("Voltage V")
plt.ylabel("Temperature deg C")
# Match the axis tick style in the screenshot: 0.1 steps on the Voltage axis.
x_min = float(np.floor(x_plot.min() * 10.0) / 10.0)
x_max = float(np.ceil(x_plot.max() * 10.0) / 10.0)
plt.xlim(x_min, x_max)
plt.xticks(np.arange(x_min, x_max + 1e-9, 0.1))
_hide_toolbar(plt.gcf())
plt.show()
if __name__ == "__main__":
main()