import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import numpy as np
from scipy.interpolate import make_interp_spline
# 1. Data
x = [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]
y = [13.92, 9.55, 4.61, 2.99, 3.24, 2.10, 2.31, 2.55, 1.38, 1.78, 2.18,
1.98, 2.91, 3.24, 3.32, 4.73, 5.54, 7.52, 9.02, 12.54, 2.59]
# 2. Smoothing
x_smooth = np.linspace(min(x), max(x), 300)
spl = make_interp_spline(x, y, k=3)
y_smooth = spl(x_smooth)
y_smooth = [val if val > 0 else 0 for val in y_smooth]
# 3. Colors
colors = []
for val in x:
if val >= 0.7: colors.append('green')
elif val <= 0.3: colors.append('firebrick')
else: colors.append('gold')
# 4. Chart
plt.figure(figsize=(7, 6))
plt.bar(x, y, width=0.04, color=colors, edgecolor='black', alpha=0.7, label='Data Frequency')
# 5. Tags
plt.xlabel('Caco-2 Permeability Probability (0=Low, 1=High)', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('Caco-2 Permeability Distribution', fontsize=14)
plt.xticks(np.arange(0, 1.1, 0.1))
plt.xlim(-0.05, 1.05)
plt.ylim(0, 16)
# 6. VERTICAL LEGEND (The change is here)
legend_elements = [
Patch(facecolor='green', edgecolor='black', alpha=0.7, label='High Permeability (> 0.7)'),
Patch(facecolor='gold', edgecolor='black', alpha=0.7, label='Moderate / Uncertain (0.3 - 0.7)'),
Patch(facecolor='firebrick', edgecolor='black', alpha=0.7, label='Low Permeability (< 0.3)')
]
# ncol=1 puts one below the other. frameon=False remove the frame if you want it to look cleaner
plt.legend(handles=legend_elements, loc='upper center', ncol=1, framealpha=0.9)
plt.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()