import numpy as np
import matplotlib.pyplot as plt

# สร้างช่วงของค่า t สำหรับกราฟ
t_values = np.linspace(-10, 10, 400)
t_dist = stats.t.pdf(t_values, df)  # คำนวณความหนาแน่นของ t-distribution

# สร้างกราฟ
plt.figure(figsize=(8, 5))
plt.plot(t_values, t_dist, label="t-Distribution", color="blue")

# แรเงาพื้นที่ของค่าขีดวิกฤติ (Critical Regions)
plt.fill_between(t_values, t_dist, where=(t_values >= t_critical), color='red', alpha=0.3, label="Rejection Region")
plt.fill_between(t_values, t_dist, where=(t_values <= -t_critical), color='red', alpha=0.3)

# เส้นค่าทดสอบ t
plt.axvline(t_stat, color="black", linestyle="dashed", label=f"t = {t_stat:.2f}")

# เส้นค่าขีดวิกฤติ
plt.axvline(t_critical, color="red", linestyle="dashed", label=f"Critical t = {t_critical:.2f}")
plt.axvline(-t_critical, color="red", linestyle="dashed")

# กำหนดชื่อแกนและชื่อกราฟ
plt.xlabel("t-value")
plt.ylabel("Density")
plt.title("T-Distribution with Critical Regions")

# แสดงคำอธิบายกราฟ
plt.legend()
plt.grid()

# แสดงกราฟ
plt.show()
