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
35 changes: 24 additions & 11 deletions plots/density-basic/implementations/plotnine.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
""" pyplots.ai
density-basic: Basic Density Plot
Library: plotnine 0.15.2 | Python 3.13.11
Quality: 91/100 | Created: 2025-12-23
Library: plotnine 0.15.3 | Python 3.14.3
Quality: 90/100 | Updated: 2026-02-23
"""

import numpy as np
import pandas as pd
from plotnine import (
aes,
after_stat,
annotate,
coord_cartesian,
element_blank,
element_line,
element_text,
geom_density,
geom_area,
geom_line,
geom_rug,
geom_vline,
ggplot,
labs,
scale_x_continuous,
scale_y_continuous,
theme,
theme_minimal,
)
Expand All @@ -29,27 +35,34 @@
np.random.normal(88, 5, 50), # High achievers
]
)
test_scores = np.clip(test_scores, 0, 100) # Scores between 0-100
test_scores = np.clip(test_scores, 0, 100)

df = pd.DataFrame({"score": test_scores})

# Plot with bandwidth adjustment for smoother curve
# Plot - layered density with bimodal emphasis
plot = (
ggplot(df, aes(x="score"))
+ geom_density(fill="#306998", color="#306998", alpha=0.6, size=1.5, bw="scott")
+ geom_rug(color="#FFD43B", alpha=0.7, size=1)
+ geom_area(aes(y=after_stat("density")), stat="density", fill="#306998", alpha=0.45, color="none")
+ geom_line(aes(y=after_stat("density")), stat="density", color="#1a3d5c", size=1.8)
+ geom_vline(xintercept=72, linetype="dashed", color="#5a6d7e", size=0.6, alpha=0.5)
+ geom_vline(xintercept=88, linetype="dashed", color="#5a6d7e", size=0.6, alpha=0.5)
+ geom_rug(color="#8B7355", alpha=0.4, size=0.7)
+ annotate("text", x=72, y=0.034, label="Main Group (μ ≈ 72)", size=11, color="#1a3d5c")
+ annotate("text", x=89, y=0.029, label="High Achievers (μ ≈ 88)", size=11, color="#6B4226")
+ labs(x="Test Score (points)", y="Probability Density", title="density-basic · plotnine · pyplots.ai")
+ scale_x_continuous(limits=(30, 100), breaks=range(30, 101, 10))
+ coord_cartesian(xlim=(30, 100))
+ scale_x_continuous(breaks=range(45, 101, 10))
+ scale_y_continuous(expand=(0, 0, 0.2, 0))
+ coord_cartesian(xlim=(45, 102))
+ 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),
panel_grid_major=element_line(color="#cccccc", alpha=0.3),
panel_grid_minor=element_line(color="#eeeeee", alpha=0.2),
panel_grid_major_x=element_blank(),
panel_grid_minor=element_blank(),
panel_grid_major_y=element_line(color="#cccccc", alpha=0.2, size=0.5),
)
)

Expand Down
Loading