-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotnine.py
More file actions
64 lines (58 loc) · 1.78 KB
/
Copy pathplotnine.py
File metadata and controls
64 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
""" pyplots.ai
density-basic: Basic Density Plot
Library: plotnine 0.15.3 | Python 3.14
Quality: /100 | Updated: 2026-02-23
"""
import numpy as np
import pandas as pd
from plotnine import (
aes,
after_stat,
coord_cartesian,
element_blank,
element_line,
element_text,
geom_area,
geom_line,
geom_rug,
ggplot,
labs,
scale_x_continuous,
scale_y_continuous,
theme,
theme_minimal,
)
# Data - simulating test scores with bimodal distribution
np.random.seed(42)
test_scores = np.concatenate(
[
np.random.normal(72, 10, 150), # Main distribution
np.random.normal(88, 5, 50), # High achievers
]
)
test_scores = np.clip(test_scores, 0, 100)
df = pd.DataFrame({"score": test_scores})
# Plot - layered density using stat_density for filled area + outline
plot = (
ggplot(df, aes(x="score"))
+ geom_area(aes(y=after_stat("density")), stat="density", fill="#306998", alpha=0.5, color="none")
+ geom_line(aes(y=after_stat("density")), stat="density", color="#1a3d5c", size=1.8)
+ geom_rug(color="#306998", alpha=0.4, size=0.8)
+ labs(x="Test Score (points)", y="Probability Density", title="density-basic · plotnine · pyplots.ai")
+ scale_x_continuous(breaks=range(40, 101, 10))
+ scale_y_continuous(expand=(0, 0, 0.05, 0))
+ coord_cartesian(xlim=(40, 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_x=element_blank(),
panel_grid_minor=element_blank(),
panel_grid_major_y=element_line(color="#cccccc", alpha=0.2, size=0.5),
)
)
# Save
plot.save("plot.png", dpi=300, verbose=False)