import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import numpy as np
from scipy.interpolate import make_interp_spline
# 1. Original Data (hERG Blocker Probability, hERG_1_10uM)
bin_centers = np.array([0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0])
frequencies = np.array([38.30, 13.63, 5.44, 3.86, 3.23, 2.80, 2.17, 1.95, 1.91, 2.08, 1.95, 1.91, 1.74, 1.91, 1.87, 2.21, 1.91, 2.00, 3.14, 4.03, 1.95])
# 2. Generate the smoothed curve (Spline)
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 = np.clip(y_smooth, 0, None)
# 3. Hexadecimal Colors
color_safe = '#008000'
color_warn = '#FFD700'
color_danger = '#B22222'
colors = []
for val in bin_centers:
if val < 0.3:
colors.append(color_safe)
elif val < 0.7:
colors.append(color_warn)
else:
colors.append(color_danger)
# 4. Create the graph
plt.figure(figsize=(7, 6))
# Bars
plt.bar(bin_centers, frequencies, width=0.04, color=colors, edgecolor='black', alpha=0.7, label='Observed Data')
# 5. Tags and Titles
plt.xlabel('Probability of hERG Blockade', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('hERG Cardiotoxicity 1-10 uM Distribution', fontsize=14)
# Axle settings
plt.xticks(np.arange(0.0, 1.05, 0.1))
plt.xlim(-0.05, 1.05)
plt.ylim(0, 42)
# 6. Legend
legend_elements = [
Patch(facecolor=color_safe, edgecolor='black', label='Low Risk (Acceptable)'),
Patch(facecolor=color_warn, edgecolor='black', label='Moderate Risk (Monitor)'),
Patch(facecolor=color_danger, edgecolor='black', label='High Risk (Arrhythmia)'),
]
plt.legend(handles=legend_elements, loc='upper right', framealpha=0.95, fontsize=10)
plt.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()