import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
# Enable LaTeX rendering
plt.rcParams['text.usetex'] = True
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.size'] = 15
# Create figure and axis
fig, ax = plt.subplots(figsize=(10, 6))
# Define the line profile parameters
v0 = 0 # Center frequency
sigma = 1 # Standard deviation for Gaussian profile
v = np.linspace(-4, 4, 1000)
# Create Gaussian profile for the spectral line
P = norm.pdf(v, v0, sigma)
P = P / P.max() # Normalize to peak at 1
# Define key positions
P0 = 1.0 # Peak intensity
P_half = 0.5 # Half maximum
# Find FWHM positions (Full Width at Half Maximum)
idx_half_left = np.argmin(np.abs(P[:500] - P_half))
idx_half_right = np.argmin(np.abs(P[500:] - P_half)) + 500
v1 = v[idx_half_left]
v2 = v[idx_half_right]
# Plot the main curve
ax.plot(v, P, 'r-', linewidth=2)
# Fill areas
# Line core (Linienkern)
core_mask = (v >= v1) & (v <= v2)
ax.fill_between(v[core_mask], 0, P[core_mask], alpha=0.5, color='salmon', label='Line core')
# Line wings (Linienflügel)
left_wing_mask = v < v1
right_wing_mask = v > v2
ax.fill_between(v[left_wing_mask], 0, P[left_wing_mask], alpha=0.3, color='lightcoral')
ax.fill_between(v[right_wing_mask], 0, P[right_wing_mask], alpha=0.3, color='lightcoral')
# Add horizontal dashed lines
ax.plot([-4,0], [P0,P0] , 'k--', linewidth=1)
ax.plot([-4,0], [P_half,P_half] , 'k--', linewidth=1)
# Add vertical lines
ax.plot([v0, v0], [0, P0], 'k-', linewidth=1.5)
ax.plot([v1, v1], [0, P_half], 'k--', linewidth=1)
ax.plot([v2, v2], [0, P_half], 'k--', linewidth=1)
# Add annotations for δν (line width)
ax.annotate('', xy=(v2, P_half), xytext=(v1, P_half),
arrowprops=dict(arrowstyle='<->', color='black', lw=1.5))
ax.text(v0 + 0.2, P_half + 0.01, r'$\delta\nu$', ha='center', fontsize=18)
# Add labels
ax.text(1.12, 0.74, r'Line core', fontsize=18, transform=ax.transData)
ax.plot([1.1, 0.5], [0.73, 0.65], 'k-', linewidth=0.5)
ax.text(2.5, 0.25, r'Line wing', fontsize=18)
ax.plot([2.45, 1.7], [0.24, 0.1], 'k-', linewidth=0.5)
# Y-axis labels
ax.text(-4.3, P0, r'$P_0$', ha='left', va='center', fontsize=15)
ax.text(-4.5, P_half, r'$P_0/2$', ha='left', va='center', fontsize=15)
# X-axis labels
ax.text(v1, -0.021, r'$\nu_1$', ha='center', va='top', fontsize=18)
ax.text(v0, -0.021, r'$\nu_0$', ha='center', va='top', fontsize=18)
ax.text(v2, -0.021, r'$\nu_2$', ha='center', va='top', fontsize=18)
# Set labels and title
ax.set_xlabel(r'$\nu$', loc='right', fontsize=16)
ax.set_ylabel(r'$P(\nu)$', loc='top', fontsize=16)
#ax.set_title(r'Line Profile of a Spectral Line', fontsize=1, pad=15)
# Set axis limits
ax.set_xlim(-4, 4)
ax.set_ylim(0, 1.15)
# Remove tick marks and labels
ax.set_xticks([])
ax.set_yticks([])
# Remove top and right spines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Set grid
ax.grid(alpha=0.3, linestyle=':', linewidth=0.5)
plt.tight_layout()
plt.show()