|
1 | 1 | """ pyplots.ai |
2 | 2 | violin-basic: Basic Violin Plot |
3 | | -Library: seaborn 0.13.2 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-23 |
| 3 | +Library: seaborn 0.13.2 | Python 3.14.3 |
| 4 | +Quality: /100 | Updated: 2026-02-21 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import matplotlib.pyplot as plt |
|
12 | 12 |
|
13 | 13 | # Data - Salary distributions across departments |
14 | 14 | np.random.seed(42) |
15 | | -categories = ["Engineering", "Marketing", "Sales", "Support"] |
16 | | -data = [] |
17 | | - |
18 | | -for cat in categories: |
19 | | - # Different distribution shapes per category |
20 | | - if cat == "Engineering": |
21 | | - values = np.random.normal(85000, 15000, 150) |
22 | | - elif cat == "Marketing": |
23 | | - values = np.random.normal(70000, 12000, 150) |
24 | | - elif cat == "Sales": |
25 | | - # Bimodal distribution for sales (junior vs senior) |
26 | | - values = np.concatenate([np.random.normal(55000, 8000, 75), np.random.normal(90000, 10000, 75)]) |
27 | | - else: # Support |
28 | | - values = np.random.normal(55000, 10000, 150) |
29 | | - |
30 | | - for v in values: |
31 | | - data.append({"Department": cat, "Salary": v}) |
32 | | - |
33 | | -df = pd.DataFrame(data) |
| 15 | +departments = ["Engineering", "Marketing", "Sales", "Support"] |
| 16 | +records = [] |
| 17 | + |
| 18 | +for dept in departments: |
| 19 | + if dept == "Engineering": |
| 20 | + salaries = np.random.normal(85000, 15000, 150) |
| 21 | + elif dept == "Marketing": |
| 22 | + salaries = np.random.normal(70000, 12000, 150) |
| 23 | + elif dept == "Sales": |
| 24 | + # Bimodal distribution (junior vs senior) — showcases KDE strength |
| 25 | + salaries = np.concatenate([np.random.normal(55000, 8000, 75), np.random.normal(90000, 10000, 75)]) |
| 26 | + else: |
| 27 | + salaries = np.random.normal(55000, 10000, 150) |
| 28 | + for s in salaries: |
| 29 | + records.append({"Department": dept, "Salary": s}) |
| 30 | + |
| 31 | +df = pd.DataFrame(records) |
34 | 32 |
|
35 | 33 | # Plot |
36 | 34 | fig, ax = plt.subplots(figsize=(16, 9)) |
|
40 | 38 | x="Department", |
41 | 39 | y="Salary", |
42 | 40 | hue="Department", |
43 | | - palette=["#306998", "#FFD43B", "#306998", "#FFD43B"], |
44 | | - inner="quart", # Show quartiles inside violin |
45 | | - linewidth=2, |
| 41 | + palette=["#306998", "#4A90C4", "#2D5F8A", "#5BA3D9"], |
| 42 | + inner="box", |
| 43 | + cut=0, |
| 44 | + linewidth=1.5, |
| 45 | + saturation=0.9, |
46 | 46 | legend=False, |
47 | 47 | ax=ax, |
48 | 48 | ) |
49 | 49 |
|
50 | 50 | # Style |
51 | 51 | ax.set_xlabel("Department", fontsize=20) |
52 | 52 | ax.set_ylabel("Salary ($)", fontsize=20) |
53 | | -ax.set_title("violin-basic · seaborn · pyplots.ai", fontsize=24) |
| 53 | +ax.set_title("violin-basic \u00b7 seaborn \u00b7 pyplots.ai", fontsize=24, fontweight="medium") |
54 | 54 | ax.tick_params(axis="both", labelsize=16) |
55 | | -ax.grid(True, alpha=0.3, linestyle="--", axis="y") |
| 55 | +ax.spines["top"].set_visible(False) |
| 56 | +ax.spines["right"].set_visible(False) |
| 57 | +ax.yaxis.grid(True, alpha=0.2, linewidth=0.8) |
| 58 | +ax.set_axisbelow(True) |
56 | 59 |
|
57 | 60 | # Format y-axis as currency |
58 | 61 | ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: f"${x / 1000:.0f}k")) |
|
0 commit comments