|
1 | 1 | """ anyplot.ai |
2 | 2 | lollipop-basic: Basic Lollipop Chart |
3 | | -Library: plotly 6.7.0 | Python 3.14.4 |
4 | | -Quality: 87/100 | Updated: 2026-04-26 |
| 3 | +Library: plotly 6.8.0 | Python 3.13.14 |
| 4 | +Quality: 92/100 | Updated: 2026-07-01 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import os |
|
15 | 15 | ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
16 | 16 | INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
17 | 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 | | -BRAND = "#009E73" # Okabe-Ito position 1 — ALWAYS first series |
| 18 | +GRID = "rgba(26,26,23,0.15)" if THEME == "light" else "rgba(240,239,232,0.15)" |
| 19 | +BRAND = "#009E73" # Imprint palette position 1 — ALWAYS first series |
| 20 | +ACCENT = "#C475FD" # Imprint palette position 2 — focal point for top performer |
20 | 21 |
|
21 | | -# Data — Product sales by category (deterministic, sorted descending) |
22 | | -categories = [ |
23 | | - "Electronics", |
24 | | - "Clothing", |
25 | | - "Home & Garden", |
26 | | - "Sports", |
27 | | - "Books", |
28 | | - "Toys", |
29 | | - "Beauty", |
30 | | - "Automotive", |
31 | | - "Food & Grocery", |
32 | | - "Health", |
| 22 | +# Data — streaming platform monthly watch hours by genre (thousands of hours, sorted descending) |
| 23 | +genres = [ |
| 24 | + "Drama", |
| 25 | + "Comedy", |
| 26 | + "Thriller", |
| 27 | + "Action", |
| 28 | + "Documentary", |
| 29 | + "Animation", |
| 30 | + "Romance", |
| 31 | + "Horror", |
| 32 | + "Reality TV", |
| 33 | + "Sci-Fi", |
33 | 34 | ] |
34 | | -values = [124820, 97340, 86715, 75260, 64480, 53905, 47620, 41370, 37815, 30945] |
| 35 | +watch_hours = [3840, 2970, 2450, 2180, 1820, 1540, 1280, 990, 820, 650] |
35 | 36 |
|
36 | 37 | # Plot |
37 | 38 | fig = go.Figure() |
38 | 39 |
|
39 | | -# Stems — one segmented Scatter trace via None separators (single trace, fewer DOM nodes) |
40 | | -stem_x = [] |
41 | | -stem_y = [] |
42 | | -for cat, val in zip(categories, values, strict=True): |
43 | | - stem_x.extend([cat, cat, None]) |
44 | | - stem_y.extend([0, val, None]) |
| 40 | +# Accent stem first — anchors Drama as the leftmost category on the x-axis |
| 41 | +fig.add_trace( |
| 42 | + go.Scatter( |
| 43 | + x=[genres[0], genres[0], None], |
| 44 | + y=[0, watch_hours[0], None], |
| 45 | + mode="lines", |
| 46 | + line={"color": ACCENT, "width": 3}, |
| 47 | + showlegend=False, |
| 48 | + hoverinfo="skip", |
| 49 | + ) |
| 50 | +) |
| 51 | + |
| 52 | +# Standard stems — brand color for positions 1–9 via None-separator trick |
| 53 | +stem_x, stem_y = [], [] |
| 54 | +for genre, hours in zip(genres[1:], watch_hours[1:], strict=True): |
| 55 | + stem_x.extend([genre, genre, None]) |
| 56 | + stem_y.extend([0, hours, None]) |
45 | 57 |
|
46 | 58 | fig.add_trace( |
47 | 59 | go.Scatter(x=stem_x, y=stem_y, mode="lines", line={"color": BRAND, "width": 3}, showlegend=False, hoverinfo="skip") |
48 | 60 | ) |
49 | 61 |
|
50 | | -# Markers — circular dots at the top of each stem |
| 62 | +# Markers — top performer in accent, rest in brand green |
| 63 | +marker_colors = [ACCENT] + [BRAND] * (len(genres) - 1) |
51 | 64 | fig.add_trace( |
52 | 65 | go.Scatter( |
53 | | - x=categories, |
54 | | - y=values, |
| 66 | + x=genres, |
| 67 | + y=watch_hours, |
55 | 68 | mode="markers", |
56 | | - marker={"color": BRAND, "size": 22, "line": {"color": PAGE_BG, "width": 2.5}, "symbol": "circle"}, |
| 69 | + marker={"color": marker_colors, "size": 22, "line": {"color": PAGE_BG, "width": 2.5}, "symbol": "circle"}, |
57 | 70 | showlegend=False, |
58 | | - hovertemplate="<b>%{x}</b><br>Sales: $%{y:,.0f}<extra></extra>", |
| 71 | + hovertemplate="<b>%{x}</b><br>%{y:,.0f}k hours/month<extra></extra>", |
59 | 72 | cliponaxis=False, |
60 | 73 | ) |
61 | 74 | ) |
62 | 75 |
|
| 76 | +# Title fontsize scaled for length (plotly default 16px, floor 11px) |
| 77 | +title_text = "Streaming Hours by Genre · lollipop-basic · python · plotly · anyplot.ai" |
| 78 | +title_fontsize = max(11, round(16 * 67 / len(title_text))) |
| 79 | + |
63 | 80 | # Style |
64 | 81 | fig.update_layout( |
| 82 | + autosize=False, |
65 | 83 | title={ |
66 | | - "text": "Product Sales by Category · lollipop-basic · plotly · anyplot.ai", |
67 | | - "font": {"size": 28, "color": INK}, |
| 84 | + "text": title_text, |
| 85 | + "font": {"size": title_fontsize, "color": INK}, |
68 | 86 | "x": 0.5, |
69 | 87 | "xanchor": "center", |
70 | | - "y": 0.95, |
| 88 | + "y": 0.97, |
71 | 89 | }, |
72 | 90 | xaxis={ |
73 | | - "title": {"text": "Product Category", "font": {"size": 22, "color": INK}}, |
74 | | - "tickfont": {"size": 18, "color": INK_SOFT}, |
| 91 | + "title": {"text": "Content Genre", "font": {"size": 12, "color": INK}}, |
| 92 | + "tickfont": {"size": 10, "color": INK_SOFT}, |
75 | 93 | "tickangle": -35, |
76 | 94 | "showgrid": False, |
| 95 | + "showline": True, |
| 96 | + "mirror": False, |
77 | 97 | "linecolor": INK_SOFT, |
78 | 98 | "ticks": "outside", |
79 | 99 | "tickcolor": INK_SOFT, |
80 | 100 | "ticklen": 6, |
81 | 101 | }, |
82 | 102 | yaxis={ |
83 | | - "title": {"text": "Sales ($)", "font": {"size": 22, "color": INK}}, |
84 | | - "tickfont": {"size": 18, "color": INK_SOFT}, |
85 | | - "tickformat": "$,.0f", |
| 103 | + "title": {"text": "Watch Hours (thousands/month)", "font": {"size": 12, "color": INK}}, |
| 104 | + "tickfont": {"size": 10, "color": INK_SOFT}, |
| 105 | + "tickformat": ",.0f", |
86 | 106 | "gridcolor": GRID, |
87 | 107 | "gridwidth": 1, |
88 | 108 | "zeroline": True, |
89 | 109 | "zerolinecolor": INK_SOFT, |
90 | 110 | "zerolinewidth": 1.5, |
| 111 | + "showline": True, |
| 112 | + "mirror": False, |
91 | 113 | "linecolor": INK_SOFT, |
92 | | - "range": [0, max(values) * 1.1], |
| 114 | + "range": [0, max(watch_hours) * 1.12], |
93 | 115 | }, |
94 | 116 | paper_bgcolor=PAGE_BG, |
95 | 117 | plot_bgcolor=PAGE_BG, |
96 | 118 | font={"color": INK, "family": "Inter, system-ui, sans-serif"}, |
97 | | - margin={"l": 110, "r": 60, "t": 110, "b": 160}, |
| 119 | + margin={"l": 110, "r": 60, "t": 80, "b": 140}, |
98 | 120 | showlegend=False, |
99 | | - hoverlabel={"bgcolor": ELEVATED_BG, "bordercolor": INK_SOFT, "font": {"color": INK, "size": 16}}, |
| 121 | + hoverlabel={"bgcolor": ELEVATED_BG, "bordercolor": INK_SOFT, "font": {"color": INK, "size": 14}}, |
100 | 122 | ) |
101 | 123 |
|
102 | | -# Save |
103 | | -fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3) |
| 124 | +# Save — canonical 3200×1800 landscape canvas |
| 125 | +fig.write_image(f"plot-{THEME}.png", width=800, height=450, scale=4) |
104 | 126 | fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn") |
0 commit comments