import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib.patches import Patch
import numpy as np
# 1. Original Data (Acute Oral Toxicity)
bins_ao = np.array([-4.4, -4.2, -4.0, -3.8, -3.6, -3.4, -3.2, -3.0, -2.8, -2.6, -2.4, -2.2, -2.0, -1.8, -1.6, -1.4, -1.2, -1.0, -0.8, -0.6, -0.4, -0.2, 0.0])
freq_ao = np.array([0.08, 0.85, 3.65, 4.08, 7.52, 12.19, 14.14, 14.18, 12.78, 13.04, 7.47, 2.72, 2.08, 1.27, 1.27, 0.59, 0.81, 0.51, 0.13, 0.13, 0.25, 0.13, 0.13])
# 2. Exact Gaussian Parameters
amp = 14.93
mean = -3.015
std = 0.5207
def gauss_exact(x, a, mu, sigma):
return a * np.exp(-((x - mu)**2) / (2 * sigma**2))
x_fit = np.linspace(-4.6, 0.2, 500)
y_fit = gauss_exact(x_fit, amp, mean, std)
# 3. Color Function (Based on EPA/GHS cuts of LD50)
def get_colors(bins):
return ['#008000' if b < -2.7 else '#FFD700' if b <= -1.7 else '#B22222' for b in bins]
colors_hex = get_colors(bins_ao)
face_colors = [mcolors.to_rgba(c, alpha=0.50) for c in colors_hex]
edge_colors = [mcolors.to_rgba(c, alpha=0.90) for c in colors_hex]
# 4. Create the chart
plt.figure(figsize=(7, 6))
# Draw bars and Gaussian line
plt.bar(bins_ao, freq_ao, width=0.18, color=face_colors, edgecolor=edge_colors, linewidth=1.5, zorder=2)
plt.plot(x_fit, y_fit, color='orange', linewidth=2.5, linestyle='-', alpha=0.8, zorder=3)
# 5. Tags and Titles
plt.xlabel(r'Acute Oral Toxicity ($-\log_{10} LD_{50}$)', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('Acute Oral Toxicity Distribution (Rat Model)', fontsize=14)
# 6. Structured Legend
legend_elements = [
Patch(facecolor=mcolors.to_rgba('#008000', 0.5), edgecolor='#008000', label='Safe / Low Risk (< -2.7)'),
Patch(facecolor=mcolors.to_rgba('#FFD700', 0.5), edgecolor='#FFD700', label='Moderate Risk (-2.7 to -1.7)'),
Patch(facecolor=mcolors.to_rgba('#B22222', 0.5), edgecolor='#B22222', label='High Toxicity/Lethal (> -1.7)'),
plt.Line2D([0], [0], color='orange', lw=2.5, alpha=0.8, label=f'Fit (Mean={mean}, SD={std})')
]
plt.legend(handles=legend_elements, loc='upper right', framealpha=0.95, fontsize=10)
plt.grid(axis='y', linestyle=':', alpha=0.7, zorder=0)
plt.xlim(-4.6, 0.2)
plt.ylim(0, 16)
plt.tight_layout()
plt.show()