import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import numpy as np
from scipy.interpolate import make_interp_spline
# 1. HLM 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]
# Frequencies in %
y = [10.36, 23.79, 15.74, 9.51, 6.31, 4.98, 4.65, 3.36, 3.11, 3.80, 2.75,
1.78, 2.06, 1.46, 2.02, 1.54, 1.09, 0.81, 0.57, 0.32]
# 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 (HLM Traffic Light - REVERSE)
colors = []
for val in x:
# High Stability (Low probability of instability)
if val <= 0.3:
colors.append('green')
# High Instability
elif val >= 0.7:
colors.append('firebrick')
# Intermediate
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('Probability of Metabolic Instability (0=Stable, 1=Unstable)', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('HLM Stability Prediction', fontsize=14)
# Axle settings
plt.xticks(np.arange(0, 1.1, 0.1))
plt.xlim(-0.05, 1.05)
plt.ylim(0, 26) # Ajuste para el pico alto de 23%
# 6. Vertical Legend (On the RIGHT, data on the left)
legend_elements = [
Patch(facecolor='green', edgecolor='black', alpha=0.7, label='Stable / Low Clearance (Score < 0.3)'),
Patch(facecolor='gold', edgecolor='black', alpha=0.7, label='Intermediate Stability (0.3 - 0.7)'),
Patch(facecolor='firebrick', edgecolor='black', alpha=0.7, label='Unstable / High Clearance (Score > 0.7)')
]
plt.legend(handles=legend_elements, loc='upper right', ncol=1, framealpha=0.9)
plt.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()