import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
# 1. Bin Centers and % Frequency Data
bin_centers = [-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
frequencies = [0.0425, 0.0, 0.0, 0.0, 0.0, 0.1274, 0.1274, 0.5520, 0.9342, 2.6752,
5.5626, 9.4692, 12.5690, 15.0318, 13.5032, 13.3758, 11.4650, 9.8938,
3.2272, 0.8917, 0.2972, 0.2548]
# 2. Gaussian Fit Parameters
amplitude = 15.22
mean = 5.791
sd = 2.679
# 3. Navigability Zones Classification (Colors)
# - Green (Optimal): 3 to 9 (Weak bases, good balance of solubility and permeability)
# - Gold (Caution): 0 to 2 and 10 to 11 (Very weak or very strong bases)
# - Firebrick (Risk): < 0 and > 11 (Extremes complicating oral absorption)
colors = []
for b in bin_centers:
if 3 <= b <= 9:
colors.append('green')
elif 0 <= b <= 2 or 10 <= b <= 11:
colors.append('gold')
else:
colors.append('firebrick')
# 4. Create Figure
plt.figure(figsize=(7, 6))
plt.bar(bin_centers, frequencies, width=0.8, color=colors, edgecolor='black', alpha=0.7)
# 5. Generate Gaussian Curve
x_fit = np.linspace(min(bin_centers) - 1, max(bin_centers) + 1, 200)
# Formula: Amplitude * exp(-0.5 * ((x - Mean)/SD)^2)
y_fit = amplitude * np.exp(-0.5 * ((x_fit - mean) / sd) ** 2)
plt.plot(x_fit, y_fit, color='darkorange', lw=2.5)
# 6. Labels and Titles
plt.xlabel('Basic pKa', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('Basic pKa Distribution with Gaussian Fit', fontsize=14)
# 7. Axes adjustments
plt.xticks(np.arange(-8, 14, 1))
plt.xlim(-8.5, 13.5)
plt.ylim(0, 18)
# 8.Custom Legend
legend_elements = [
Line2D([0], [0], color='darkorange', lw=2.5, label=f'Fit (Mean={mean}, SD={sd})'),
Patch(facecolor='green', edgecolor='black', alpha=0.7, label='Optimal (pKa 3 - 9)'),
Patch(facecolor='gold', edgecolor='black', alpha=0.7, label='Caution (pKa 0-2, 10-11)'),
Patch(facecolor='firebrick', edgecolor='black', alpha=0.7, label='Risk / Extremes (< 0, > 11)')
]
plt.legend(handles=legend_elements, loc='upper left')
# 9. Final Styling
plt.grid(axis='y', linestyle=':', alpha=0.7, zorder=0)
plt.tight_layout()
# Show plot
plt.show()