(data: np.ndarray, title: str, xlabel: str, output_path: Path)
| 315 | |
| 316 | |
| 317 | def create_histogram(data: np.ndarray, title: str, xlabel: str, output_path: Path): |
| 318 | if len(data) == 0: |
| 319 | return |
| 320 | |
| 321 | mean_val = np.mean(data) |
| 322 | std_val = np.std(data) |
| 323 | |
| 324 | fig, ax = plt.subplots(figsize=(10, 6)) |
| 325 | |
| 326 | counts, bins, patches = ax.hist(data, bins=24, alpha=0.7, color="blue", edgecolor="black") |
| 327 | |
| 328 | if std_val > 1e-10 and data.max() > data.min(): |
| 329 | x = np.linspace(data.min(), data.max(), 100) |
| 330 | normal_curve = scipy_stats.norm.pdf(x, mean_val, std_val) * len(data) * (bins[1] - bins[0]) |
| 331 | ax.plot(x, normal_curve, "g-", linewidth=2, label="Normal Distribution") |
| 332 | |
| 333 | ax.axvline(mean_val, color="red", linestyle="--", linewidth=2, label=f"Mean: {mean_val:.2f}") |
| 334 | |
| 335 | if std_val > 1e-10: |
| 336 | for i in [-3, 3]: |
| 337 | val = mean_val + i * std_val |
| 338 | ax.axvline( |
| 339 | val, |
| 340 | color="lightblue", |
| 341 | linestyle="--", |
| 342 | linewidth=1.5, |
| 343 | label=f'{"+3" if i > 0 else "-3"}σ: {val:.2f}', |
| 344 | ) |
| 345 | |
| 346 | ax.set_xlabel(xlabel) |
| 347 | ax.set_ylabel("Frequency") |
| 348 | ax.set_title(title) |
| 349 | ax.legend(title=f"μ={mean_val:.2f}, σ={std_val:.2f}") |
| 350 | ax.grid(True, alpha=0.3) |
| 351 | |
| 352 | plt.tight_layout() |
| 353 | plt.savefig(output_path, dpi=150, bbox_inches="tight") |
| 354 | plt.close() |
| 355 | |
| 356 | |
| 357 | def create_scatterplot( |
no outgoing calls
no test coverage detected