|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | dumbbell-basic: Basic Dumbbell Chart |
3 | | -Library: plotly 6.5.0 | Python 3.13.11 |
4 | | -Quality: 93/100 | Created: 2025-12-23 |
| 3 | +Library: plotly 6.7.0 | Python 3.14.4 |
| 4 | +Quality: 87/100 | Updated: 2026-04-26 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import plotly.graph_objects as go |
8 | 10 |
|
9 | 11 |
|
10 | | -# Data - Employee satisfaction scores before and after policy changes |
| 12 | +# Theme-adaptive chrome tokens |
| 13 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 14 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 15 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 16 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 17 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 18 | +GRID = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)" |
| 19 | + |
| 20 | +# Okabe-Ito palette (theme-independent data colors) |
| 21 | +BEFORE_COLOR = "#009E73" # position 1 — first categorical series |
| 22 | +AFTER_COLOR = "#D55E00" # position 2 — second series |
| 23 | + |
| 24 | +# Data — Employee satisfaction scores before and after policy changes. |
| 25 | +# Deterministic, hand-picked values (no RNG): one department (Legal) shows a |
| 26 | +# slight regression to demonstrate full data range including negative change. |
11 | 27 | categories = [ |
12 | 28 | "Engineering", |
13 | 29 | "Sales", |
|
17 | 33 | "Human Resources", |
18 | 34 | "Operations", |
19 | 35 | "Product", |
| 36 | + "Legal", |
20 | 37 | ] |
21 | | -before = [62, 71, 58, 45, 68, 52, 64, 73] # Before policy changes |
22 | | -after = [78, 82, 75, 69, 74, 71, 79, 85] # After policy changes |
| 38 | +before = [62, 71, 58, 45, 68, 52, 64, 73, 70] |
| 39 | +after = [78, 82, 75, 69, 74, 71, 79, 85, 67] |
23 | 40 |
|
24 | | -# Sort by difference (largest improvement at top of chart) |
| 41 | +# Sort by difference (largest improvement at top, regression at bottom) |
25 | 42 | data = sorted(zip(categories, before, after, strict=True), key=lambda x: x[2] - x[1]) |
26 | 43 | categories = [d[0] for d in data] |
27 | 44 | before = [d[1] for d in data] |
28 | 45 | after = [d[2] for d in data] |
29 | 46 |
|
30 | | -# Create figure |
31 | 47 | fig = go.Figure() |
32 | 48 |
|
33 | | -# Add connecting lines (one per category) |
| 49 | +# Connecting lines (one per category) |
34 | 50 | for i, cat in enumerate(categories): |
35 | 51 | fig.add_trace( |
36 | 52 | go.Scatter( |
37 | 53 | x=[before[i], after[i]], |
38 | 54 | y=[cat, cat], |
39 | 55 | mode="lines", |
40 | | - line={"color": "#999999", "width": 2}, |
| 56 | + line={"color": INK_SOFT, "width": 2}, |
41 | 57 | showlegend=False, |
42 | 58 | hoverinfo="skip", |
43 | 59 | ) |
44 | 60 | ) |
45 | 61 |
|
46 | | -# Add "Before" dots (Python Blue) |
| 62 | +# "Before" markers — Okabe-Ito green (brand) |
47 | 63 | fig.add_trace( |
48 | 64 | go.Scatter( |
49 | 65 | x=before, |
50 | 66 | y=categories, |
51 | 67 | mode="markers", |
52 | | - marker={"size": 18, "color": "#306998"}, |
| 68 | + marker={"size": 18, "color": BEFORE_COLOR, "line": {"color": PAGE_BG, "width": 2}}, |
53 | 69 | name="Before", |
54 | | - hovertemplate="<b>%{y}</b><br>Before: %{x}<extra></extra>", |
| 70 | + hovertemplate="<b>%{y}</b><br>Before: %{x}/100<extra></extra>", |
55 | 71 | ) |
56 | 72 | ) |
57 | 73 |
|
58 | | -# Add "After" dots (Python Yellow) |
| 74 | +# "After" markers — Okabe-Ito vermillion |
59 | 75 | fig.add_trace( |
60 | 76 | go.Scatter( |
61 | 77 | x=after, |
62 | 78 | y=categories, |
63 | 79 | mode="markers", |
64 | | - marker={"size": 18, "color": "#FFD43B"}, |
| 80 | + marker={"size": 18, "color": AFTER_COLOR, "line": {"color": PAGE_BG, "width": 2}}, |
65 | 81 | name="After", |
66 | | - hovertemplate="<b>%{y}</b><br>After: %{x}<extra></extra>", |
| 82 | + hovertemplate="<b>%{y}</b><br>After: %{x}/100<extra></extra>", |
67 | 83 | ) |
68 | 84 | ) |
69 | 85 |
|
70 | | -# Layout |
71 | 86 | fig.update_layout( |
72 | 87 | title={ |
73 | 88 | "text": "Employee Satisfaction · dumbbell-basic · plotly · pyplots.ai", |
74 | | - "font": {"size": 28}, |
| 89 | + "font": {"size": 28, "color": INK}, |
75 | 90 | "x": 0.5, |
76 | 91 | "xanchor": "center", |
77 | 92 | }, |
78 | 93 | xaxis={ |
79 | | - "title": {"text": "Satisfaction Score", "font": {"size": 22}}, |
80 | | - "tickfont": {"size": 18}, |
| 94 | + "title": {"text": "Satisfaction Score (out of 100)", "font": {"size": 22, "color": INK}}, |
| 95 | + "tickfont": {"size": 18, "color": INK_SOFT}, |
81 | 96 | "range": [35, 95], |
82 | | - "gridcolor": "rgba(0,0,0,0.1)", |
| 97 | + "gridcolor": GRID, |
| 98 | + "gridwidth": 1, |
| 99 | + "linecolor": INK_SOFT, |
| 100 | + "zerolinecolor": INK_SOFT, |
| 101 | + }, |
| 102 | + yaxis={ |
| 103 | + "title": {"text": "Department", "font": {"size": 22, "color": INK}}, |
| 104 | + "tickfont": {"size": 18, "color": INK_SOFT}, |
| 105 | + "gridcolor": GRID, |
83 | 106 | "gridwidth": 1, |
| 107 | + "linecolor": INK_SOFT, |
| 108 | + "zerolinecolor": INK_SOFT, |
| 109 | + "showgrid": True, |
| 110 | + }, |
| 111 | + paper_bgcolor=PAGE_BG, |
| 112 | + plot_bgcolor=PAGE_BG, |
| 113 | + font={"color": INK}, |
| 114 | + legend={ |
| 115 | + "orientation": "h", |
| 116 | + "yanchor": "bottom", |
| 117 | + "y": 1.02, |
| 118 | + "xanchor": "center", |
| 119 | + "x": 0.5, |
| 120 | + "font": {"size": 16, "color": INK_SOFT}, |
| 121 | + "bgcolor": ELEVATED_BG, |
| 122 | + "bordercolor": INK_SOFT, |
| 123 | + "borderwidth": 1, |
84 | 124 | }, |
85 | | - yaxis={"title": {"text": "Department", "font": {"size": 22}}, "tickfont": {"size": 18}}, |
86 | | - template="plotly_white", |
87 | | - legend={"orientation": "h", "yanchor": "bottom", "y": 1.02, "xanchor": "center", "x": 0.5, "font": {"size": 18}}, |
88 | | - margin={"l": 150, "r": 50, "t": 100, "b": 80}, |
| 125 | + margin={"l": 170, "r": 60, "t": 110, "b": 90}, |
89 | 126 | ) |
90 | 127 |
|
91 | | -# Save |
92 | | -fig.write_image("plot.png", width=1600, height=900, scale=3) |
93 | | -fig.write_html("plot.html", include_plotlyjs="cdn") |
| 128 | +fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3) |
| 129 | +fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn") |
0 commit comments