import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import numpy as np
from scipy.interpolate import make_interp_spline
# 1. CYP3A4 Substrate Data
bin_centers = [0.0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40,
0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.0]
frequencies = [5.02, 9.99, 8.78, 5.87, 5.14, 4.09, 3.48, 3.60, 2.47,
2.79, 2.43, 3.32, 3.56, 3.72, 4.17, 5.22, 5.91, 6.72, 7.44, 5.22, 1.09]
# 2. Smoothing
x_smooth = np.linspace(min(bin_centers), max(bin_centers), 300)
spl = make_interp_spline(bin_centers, frequencies, k=3)
y_smooth = spl(x_smooth)
y_smooth = [val if val > 0 else 0 for val in y_smooth]
# 3. Colors (Bimodal: Blue vs Orange. Functional distinction, not risk.)
colors = []
for val in bin_centers:
if val < 0.3:
colors.append('skyblue')
elif val > 0.65:
colors.append('darkorange')
else:
colors.append('lightgray')
# 4. Create the chart
plt.figure(figsize=(7, 6))
# Bars
plt.bar(bin_centers, frequencies, width=0.04, color=colors, edgecolor='black', alpha=0.8, label='Data Frequency')
# 5. Tags and Titles
plt.xlabel('Probability of being a CYP3A4 Substrate', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('CYP3A4: The Main Metabolic Highway', fontsize=14)
# Axle settings
plt.xticks(np.arange(0.0, 1.05, 0.1))
plt.xlim(-0.05, 1.05)
plt.ylim(0, 13)
# 6. Legend (On the right, as you requested)
legend_elements = [
Patch(facecolor='skyblue', edgecolor='black', label='Non-Substrate (Uses other routes)'),
Patch(facecolor='lightgray', edgecolor='black', label='Mixed Potential'),
Patch(facecolor='darkorange', edgecolor='black', label='CYP3A4 Substrate (Standard)'),
]
plt.legend(handles=legend_elements, loc='upper right', framealpha=0.95, ncol=1, fontsize=10)
plt.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()