Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions plots/line-loss-training/implementations/plotnine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
""" pyplots.ai
line-loss-training: Training Loss Curve
Library: plotnine 0.15.2 | Python 3.13.11
Quality: 91/100 | Created: 2025-12-31
"""

import numpy as np
import pandas as pd
from plotnine import (
aes,
annotate,
element_line,
element_text,
geom_line,
geom_point,
geom_vline,
ggplot,
labs,
scale_color_manual,
theme,
theme_minimal,
)


# Data - Simulated training history with typical loss curve behavior
np.random.seed(42)
epochs = np.arange(1, 51)

# Training loss: starts high, decreases with diminishing returns
train_loss = 2.5 * np.exp(-0.08 * epochs) + 0.15 + np.random.normal(0, 0.02, len(epochs))

# Validation loss: follows training initially, then diverges (overfitting)
val_loss = 2.5 * np.exp(-0.06 * epochs) + 0.25 + np.random.normal(0, 0.03, len(epochs))
# Add uptick after epoch 30 to show clear overfitting
val_loss[30:] += np.linspace(0, 0.25, 20)

# Find optimal stopping point (minimum validation loss)
optimal_epoch = epochs[np.argmin(val_loss)]

# Create long-format DataFrame for plotnine
df = pd.DataFrame(
{
"Epoch": np.concatenate([epochs, epochs]),
"Loss": np.concatenate([train_loss, val_loss]),
"Type": ["Training Loss"] * len(epochs) + ["Validation Loss"] * len(epochs),
}
)

# Plot
plot = (
ggplot(df, aes(x="Epoch", y="Loss", color="Type"))
+ geom_line(size=1.5, alpha=0.9)
+ geom_point(size=3, alpha=0.7)
+ geom_vline(xintercept=optimal_epoch, linetype="dashed", color="#555555", size=0.8, alpha=0.7)
+ annotate(
"text",
x=optimal_epoch + 1,
y=max(val_loss) * 0.85,
label=f"Best: {optimal_epoch}",
size=12,
ha="left",
color="#555555",
)
+ scale_color_manual(values={"Training Loss": "#306998", "Validation Loss": "#FFD43B"})
+ labs(title="line-loss-training · plotnine · pyplots.ai", x="Epoch", y="Cross-Entropy Loss", color="")
+ theme_minimal()
+ theme(
figure_size=(16, 9),
text=element_text(size=14),
axis_title=element_text(size=20),
axis_text=element_text(size=16),
plot_title=element_text(size=24, weight="bold"),
legend_text=element_text(size=16),
legend_position="top",
legend_direction="horizontal",
panel_grid_major=element_line(color="#cccccc", size=0.5, alpha=0.3),
panel_grid_minor=element_line(color="#eeeeee", size=0.3, alpha=0.2),
)
)

# Save
plot.save("plot.png", dpi=300, verbose=False)
28 changes: 28 additions & 0 deletions plots/line-loss-training/metadata/plotnine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
library: plotnine
specification_id: line-loss-training
created: '2025-12-31T00:35:43Z'
updated: '2025-12-31T00:38:02Z'
generated_by: claude-opus-4-5-20251101
workflow_run: 20608693967
issue: 2860
python_version: 3.13.11
library_version: 0.15.2
preview_url: https://storage.googleapis.com/pyplots-images/plots/line-loss-training/plotnine/plot.png
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/line-loss-training/plotnine/plot_thumb.png
preview_html: null
quality_score: 91
review:
strengths:
- Excellent visualization of training dynamics with clear overfitting pattern visible
after epoch 30
- Proper use of plotnine grammar of graphics with clean, readable code structure
- Smart annotation showing optimal stopping point with vertical dashed line
- Well-chosen color scheme (blue/yellow) that is colorblind-accessible
- Perfect title format and comprehensive axis labeling with loss function specified
weaknesses:
- Grid line visibility is inconsistent - some lines appear more prominent than others
despite same alpha settings
- 'The Best: 46 annotation could be positioned better - currently placed at a fixed
y-position that may not scale well with different data'
- Yellow color for validation loss could benefit from slightly darker shade for
better contrast against white background