import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import numpy as np
# 1. Original Data (Mouse Carcinogenicity)
bin_centers = np.array([0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85])
frequencies = np.array([0.04, 0.51, 2.46, 8.58, 15.54, 17.54, 14.23, 10.53, 9.13, 6.96, 5.14, 3.69, 2.08, 2.25, 1.02, 0.21, 0.08])
# 2. Exact Parameters
amp = 15.81
mean = 0.3310
std_dev = 0.1207
# Manual Gaussian function
def gauss_exact(x, a, mu, sigma):
return a * np.exp(-((x - mu)**2) / (2 * sigma**2))
# Generate data for the fit line
x_fit = np.linspace(0, 0.95, 500)
y_fit = gauss_exact(x_fit, amp, mean, std_dev)
# 3. Colors
colors = []
for val in bin_centers:
if val < 0.45:
colors.append('green')
elif val < 0.70:
colors.append('gold')
else:
colors.append('firebrick')
# 4. Create the graph
plt.figure(figsize=(7, 6))
# Bars
plt.bar(bin_centers, frequencies, width=0.04, color=colors, edgecolor='black', alpha=0.6, label='Observed Data')
# Trend Line (Exact Gaussian)
plt.plot(x_fit, y_fit, color='orange', linewidth=3,
label=f'Gaussian Fit ($R^2=0.8964$)\n$\\mu={mean:.4f}, \\sigma={std_dev:.4f}$')
# 5. Tags and Titles
plt.xlabel('Probability of Mouse Carcinogenicity', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('Mouse Carcinogenicity Distribution', fontsize=14)
# 6. Legend
legend_elements = [
Patch(facecolor='green', edgecolor='black', label=r'Normal Zone'),
Patch(facecolor='gold', edgecolor='black', label=r'Warning Zone'),
Patch(facecolor='firebrick', edgecolor='black', label=r'Danger Zone'),
plt.Line2D([0], [0], color='orange', lw=2.5, label=f'Fit (Mean={mean}, SD={std_dev})')
]
plt.legend(handles=legend_elements, loc='upper right', framealpha=0.95, fontsize=10)
plt.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()