|
1 | 1 | """ pyplots.ai |
2 | 2 | violin-basic: Basic Violin 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.3 |
| 4 | +Quality: 92/100 | Updated: 2026-02-21 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import pandas as pd |
9 | | -from plotnine import aes, element_text, geom_violin, ggplot, labs, scale_fill_manual, theme, theme_minimal |
| 9 | +from plotnine import ( |
| 10 | + aes, |
| 11 | + element_blank, |
| 12 | + element_line, |
| 13 | + element_rect, |
| 14 | + element_text, |
| 15 | + geom_violin, |
| 16 | + ggplot, |
| 17 | + labs, |
| 18 | + scale_fill_manual, |
| 19 | + theme, |
| 20 | + theme_minimal, |
| 21 | +) |
10 | 22 |
|
11 | 23 |
|
12 | 24 | # Data |
13 | 25 | np.random.seed(42) |
14 | 26 |
|
15 | | -categories = ["Group A", "Group B", "Group C", "Group D"] |
16 | | -data = [] |
| 27 | +records = [] |
| 28 | + |
| 29 | +# Biology: right-skewed (many average, few high scorers) |
| 30 | +scores_bio = np.concatenate([np.random.normal(68, 6, 150), np.random.normal(85, 3, 30)]) |
| 31 | +records.extend([("Biology", s) for s in scores_bio]) |
| 32 | + |
| 33 | +# Statistics: bimodal (two distinct clusters) |
| 34 | +scores_stat = np.concatenate([np.random.normal(55, 5, 100), np.random.normal(82, 5, 100)]) |
| 35 | +records.extend([("Statistics", s) for s in scores_stat]) |
17 | 36 |
|
18 | | -# Generate data with different distributions for each category |
19 | | -distributions = { |
20 | | - "Group A": (50, 10), # mean, std |
21 | | - "Group B": (65, 15), |
22 | | - "Group C": (45, 8), |
23 | | - "Group D": (70, 12), |
24 | | -} |
| 37 | +# Chemistry: tight normal (consistent performance) |
| 38 | +scores_chem = np.random.normal(74, 4, 200) |
| 39 | +records.extend([("Chemistry", s) for s in scores_chem]) |
25 | 40 |
|
26 | | -for category, (mean, std) in distributions.items(): |
27 | | - n_points = 200 |
28 | | - values = np.random.normal(mean, std, n_points) |
29 | | - data.extend([(category, v) for v in values]) |
| 41 | +# Psychology: wide spread (high variance) |
| 42 | +scores_psych = np.random.normal(70, 14, 200) |
| 43 | +records.extend([("Psychology", s) for s in scores_psych]) |
| 44 | + |
| 45 | +df = pd.DataFrame(records, columns=["course", "score"]) |
| 46 | +df["score"] = df["score"].clip(0, 100) |
| 47 | +df["course"] = pd.Categorical( |
| 48 | + df["course"], categories=["Biology", "Statistics", "Chemistry", "Psychology"], ordered=True |
| 49 | +) |
30 | 50 |
|
31 | | -df = pd.DataFrame(data, columns=["category", "value"]) |
| 51 | +# Custom palette: Python Blue for focal bimodal distribution, muted tones for context |
| 52 | +palette = {"Biology": "#7BAE7F", "Statistics": "#306998", "Chemistry": "#E8A87C", "Psychology": "#B8B3D6"} |
32 | 53 |
|
33 | 54 | # Plot |
34 | 55 | plot = ( |
35 | | - ggplot(df, aes(x="category", y="value", fill="category")) |
36 | | - + geom_violin(draw_quantiles=[0.25, 0.5, 0.75], size=1) |
37 | | - + scale_fill_manual(values=["#306998", "#FFD43B", "#4B8BBE", "#FFE873"]) |
38 | | - + labs(x="Category", y="Value", title="violin-basic · plotnine · pyplots.ai") |
| 56 | + ggplot(df, aes(x="course", y="score", fill="course")) |
| 57 | + + geom_violin(draw_quantiles=[0.25, 0.5, 0.75], alpha=0.82, color="#666666", size=0.35, trim=False) |
| 58 | + + scale_fill_manual(values=palette) |
| 59 | + + labs(x="Course", y="Final Exam Score (pts)", title="violin-basic \u00b7 plotnine \u00b7 pyplots.ai") |
39 | 60 | + theme_minimal() |
40 | 61 | + theme( |
41 | 62 | figure_size=(16, 9), |
42 | | - text=element_text(size=14), |
43 | | - axis_title=element_text(size=20), |
44 | | - axis_text=element_text(size=16), |
45 | | - plot_title=element_text(size=24), |
| 63 | + text=element_text(size=14, color="#333333"), |
| 64 | + axis_title=element_text(size=20, color="#333333"), |
| 65 | + axis_text=element_text(size=16, color="#444444"), |
| 66 | + plot_title=element_text(size=24, color="#222222", weight="bold"), |
46 | 67 | legend_position="none", |
| 68 | + panel_grid_major_x=element_blank(), |
| 69 | + panel_grid_minor=element_blank(), |
| 70 | + panel_grid_major_y=element_line(color="#dedede", size=0.3), |
| 71 | + plot_background=element_rect(fill="#fafafa", color="none"), |
| 72 | + panel_background=element_rect(fill="#fafafa", color="none"), |
47 | 73 | ) |
48 | 74 | ) |
49 | 75 |
|
|
0 commit comments