import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
import numpy as np
# 1. MRT Data (-log h)
bin_centers = [-2.6, -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.04, 0.20, 0.20, 0.61, 1.78, 3.48, 4.37, 8.45, 10.84, 14.85, 17.31, 15.86, 11.37, 7.00, 2.06, 1.05, 0.16, 0.24, 0.12]
# 2. Gaussian Parameters (Provided)
amplitude = 16.79
mean = -0.6231
sd = 0.4679
# 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 (Retention Gradient)
colors = []
for val in bin_centers:
if val > 0: # < 1 hour (Fast)
colors.append('steelblue')
elif val < -1.2: # > 15 hours (Slow)
colors.append('firebrick')
else:
colors.append('mediumseagreen') # Standard
# 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('Mean Retention Time Log Scale (-log h)', fontsize=12)
ax1.set_ylabel('% Frequency', fontsize=12)
ax1.set_title('Mean Retention Time (MRT) Distribution', fontsize=14)
# Mean Line
ax1.axvline(x=mean, color='black', linestyle='--', alpha=0.8, label=f'Mean (-0.62 ≈ 4.2 hours)')
# 6. Secondary Axis (Real Hours)
ax2 = ax1.twiny()
ax2.set_xlim(ax1.get_xlim())
# Key points: -2=100h, -1=10h, -0.6=4h, 0=1h, 0.6=0.25h
tick_locs = [-2, -1.3, -1, -0.62, 0, 0.6]
tick_labels = ["100h", "20h", "10h", "~4.2h", "1h", "15min"]
ax2.set_xticks(tick_locs)
ax2.set_xticklabels(tick_labels, fontweight='bold', color='darkgreen')
ax2.set_xlabel('Real Time Equivalent', color='darkgreen', fontsize=11)
# 7. Legend
legend_elements = [
Patch(facecolor='firebrick', edgecolor='black', alpha=0.7, label='Long Retention (> 15h)'),
Patch(facecolor='mediumseagreen', edgecolor='black', alpha=0.7, label='Standard Retention (1h - 15h)'),
Patch(facecolor='steelblue', edgecolor='black', alpha=0.7, label='Short Retention (< 1h)'),
Line2D([0], [0], color='orange', lw=2, label='Gaussian Fit'),
Line2D([0], [0], color='black', linestyle='--', label='Mean (~4.2 hours)')
]
ax1.legend(handles=legend_elements, loc='upper left', framealpha=0.95)
ax1.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()