import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
import numpy as np
from scipy.stats import norm
from scipy.interpolate import PchipInterpolator, make_interp_spline
# 1. BBB frequency data
datos_crudos = """Bin Center % Frequency
0 0.24
0.05 5.14
0.1 3.56
0.15 3.68
0.2 2.91
0.25 2.55
0.3 3.20
0.35 2.47
0.4 2.35
0.45 2.39
0.5 2.63
0.55 2.51
0.6 2.91
0.65 3.11
0.7 2.95
0.75 4.41
0.8 5.18
0.85 5.99
0.9 7.81
0.95 20.31
1 13.71"""
lineas = datos_crudos.strip().split('\n')[1:]
bins_array = []
freq_array = []
for linea in lineas:
b, f = linea.strip().split()
bins_array.append(float(b))
freq_array.append(float(f))
x = np.array(bins_array)
y = np.array(freq_array)
bins = x
freq = y
# 2. Curve smoothing (Spline)
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. Define colors (BBB Traffic Light)
colors = []
for val in x:
# High probability of crossing BBB (Green = BBB+)
if val >= 0.8:
colors.append('green')
# Low probability (Red = BBB-)
elif val <= 0.3:
colors.append('firebrick')
# intermediate zone
else:
colors.append('gold')
# 4. Create the chart
plt.figure(figsize=(7, 6))
# A. Bars
plt.bar(x, y, width=0.04, color=colors, edgecolor='black', alpha=0.7, label='Data Frequency')
# 5. Tags and Titles
plt.xlabel('Blood-Brain Barrier Permeability Prob. (0=No, 1=Yes)', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('BBB Permeability Distribution', fontsize=14)
# Axle settings
plt.xticks(np.arange(0, 1.1, 0.1))
plt.xlim(-0.05, 1.05)
plt.ylim(0, 22)
# 6. Vertical Legend (To the LEFT so as not to cover the right peak)
legend_elements = [
Patch(facecolor='green', edgecolor='black', alpha=0.7, label='BBB+ / CNS Active (> 0.8)'),
Patch(facecolor='gold', edgecolor='black', alpha=0.7, label='Uncertain / Selective (0.3 - 0.8)'),
Patch(facecolor='firebrick', edgecolor='black', alpha=0.7, label='BBB- / Periphery Only (< 0.3)')
]
plt.legend(handles=legend_elements, loc='upper left', ncol=1, framealpha=0.9)
plt.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()