|
1 | 1 | """ pyplots.ai |
2 | 2 | density-basic: Basic Density Plot |
3 | | -Library: plotnine 0.15.2 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: plotnine 0.15.3 | Python 3.14 |
| 4 | +Quality: /100 | Updated: 2026-02-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import pandas as pd |
9 | 9 | from plotnine import ( |
10 | 10 | aes, |
| 11 | + after_stat, |
11 | 12 | coord_cartesian, |
| 13 | + element_blank, |
12 | 14 | element_line, |
13 | 15 | element_text, |
14 | | - geom_density, |
| 16 | + geom_area, |
| 17 | + geom_line, |
15 | 18 | geom_rug, |
16 | 19 | ggplot, |
17 | 20 | labs, |
18 | 21 | scale_x_continuous, |
| 22 | + scale_y_continuous, |
19 | 23 | theme, |
20 | 24 | theme_minimal, |
21 | 25 | ) |
|
29 | 33 | np.random.normal(88, 5, 50), # High achievers |
30 | 34 | ] |
31 | 35 | ) |
32 | | -test_scores = np.clip(test_scores, 0, 100) # Scores between 0-100 |
| 36 | +test_scores = np.clip(test_scores, 0, 100) |
33 | 37 |
|
34 | 38 | df = pd.DataFrame({"score": test_scores}) |
35 | 39 |
|
36 | | -# Plot with bandwidth adjustment for smoother curve |
| 40 | +# Plot - layered density using stat_density for filled area + outline |
37 | 41 | plot = ( |
38 | 42 | ggplot(df, aes(x="score")) |
39 | | - + geom_density(fill="#306998", color="#306998", alpha=0.6, size=1.5, bw="scott") |
40 | | - + geom_rug(color="#FFD43B", alpha=0.7, size=1) |
| 43 | + + geom_area(aes(y=after_stat("density")), stat="density", fill="#306998", alpha=0.5, color="none") |
| 44 | + + geom_line(aes(y=after_stat("density")), stat="density", color="#1a3d5c", size=1.8) |
| 45 | + + geom_rug(color="#306998", alpha=0.4, size=0.8) |
41 | 46 | + labs(x="Test Score (points)", y="Probability Density", title="density-basic · plotnine · pyplots.ai") |
42 | | - + scale_x_continuous(limits=(30, 100), breaks=range(30, 101, 10)) |
43 | | - + coord_cartesian(xlim=(30, 100)) |
| 47 | + + scale_x_continuous(breaks=range(40, 101, 10)) |
| 48 | + + scale_y_continuous(expand=(0, 0, 0.05, 0)) |
| 49 | + + coord_cartesian(xlim=(40, 102)) |
44 | 50 | + theme_minimal() |
45 | 51 | + theme( |
46 | 52 | figure_size=(16, 9), |
47 | 53 | text=element_text(size=14), |
48 | 54 | axis_title=element_text(size=20), |
49 | 55 | axis_text=element_text(size=16), |
50 | 56 | plot_title=element_text(size=24), |
51 | | - panel_grid_major=element_line(color="#cccccc", alpha=0.3), |
52 | | - panel_grid_minor=element_line(color="#eeeeee", alpha=0.2), |
| 57 | + panel_grid_major_x=element_blank(), |
| 58 | + panel_grid_minor=element_blank(), |
| 59 | + panel_grid_major_y=element_line(color="#cccccc", alpha=0.2, size=0.5), |
53 | 60 | ) |
54 | 61 | ) |
55 | 62 |
|
|
0 commit comments