import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
import numpy as np
# 1. T1/2 Data (-log h)
bin_centers = [-2.4, -2.2, -2.0, -1.8, -1.6, -1.4, -1.2, -1.0, -0.8, -0.6,
-0.4, -0.2, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
frequencies = [0.16, 0.12, 0.77, 1.70, 3.44, 4.77, 7.61, 11.77, 15.82, 17.31,
17.35, 11.17, 5.22, 1.86, 0.69, 0.12, 0.08, 0.04]
# 2. Gaussian Parameters (Provided)
amplitude = 17.72
mean = -0.6335
sd = 0.4413
# Generate smooth curve
x_smooth = np.linspace(min(bin_centers), max(bin_centers), 300)
y_smooth = amplitude * np.exp(-0.5 * ((x_smooth - mean) / sd)**2)
# 3. Colors (Time Gradient)
colors = []
for val in bin_centers:
if val > 0: # < 1 hour
colors.append('steelblue') # Fast
elif val < -1.0: # > 10 hours
colors.append('firebrick') # Slow / Cumulative
else:
colors.append('mediumseagreen') # Standard (1-10h)
# 4. Create the plot
fig, ax1 = plt.subplots(figsize=(7, 6))
# A. Bars
ax1.bar(bin_centers, frequencies, width=0.18, color=colors, edgecolor='black', alpha=0.7, label='Data Frequency')
# B. Gaussian Curve
ax1.plot(x_smooth, y_smooth, color='orange', linewidth=2, label='Gaussian Fit')
# 5. Labels and Titles
ax1.set_xlabel('Half-Life Log Scale (-log h)', fontsize=12)
ax1.set_ylabel('% Frequency', fontsize=12)
ax1.set_title('Half-Life (T1/2) Distribution', fontsize=14)
# Mean Line
ax1.axvline(x=mean, color='black', linestyle='--', alpha=0.8, label=f'Mean (-0.63 ≈ 4.3 hours)')
# 6. TRICK: Second X-Axis to show REAL HOURS
ax2 = ax1.twiny()
ax2.set_xlim(ax1.get_xlim())
# Define key points in log and their labels in hours
tick_locs = [-2, -1.3, -1, -0.63, 0, 0.4]
tick_labels = ["100h", "20h", "10h", "~4h", "1h", "24min"]
ax2.set_xticks(tick_locs)
ax2.set_xticklabels(tick_labels, fontweight='bold', color='darkred')
ax2.set_xlabel('Real Time Equivalent', color='darkred', fontsize=11)
# 7. Legend
legend_elements = [
Patch(facecolor='firebrick', edgecolor='black', alpha=0.7, label='Long (> 10h)'),
Patch(facecolor='mediumseagreen', edgecolor='black', alpha=0.7, label='Standard (1h - 10h)'),
Patch(facecolor='steelblue', edgecolor='black', alpha=0.7, label='Short (< 1h)'),
Line2D([0], [0], color='orange', lw=2, label='Gaussian Fit'),
Line2D([0], [0], color='black', linestyle='--', label='Mean (~4.3 hours)')
]
ax1.legend(handles=legend_elements, loc='upper left', framealpha=0.9)
ax1.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()