|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | slope-basic: Basic Slope Chart (Slopegraph) |
3 | | -Library: bokeh 3.8.1 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: bokeh 3.9.0 | Python 3.13.13 |
| 4 | +Quality: 84/100 | Updated: 2026-04-30 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | from bokeh.io import export_png, save |
8 | | -from bokeh.models import Label |
| 10 | +from bokeh.models import ColumnDataSource, HoverTool, Label |
9 | 11 | from bokeh.plotting import figure |
10 | 12 | from bokeh.resources import CDN |
11 | 13 |
|
12 | 14 |
|
13 | | -# Data - Product sales comparison Q1 vs Q4 (10 products) |
| 15 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 16 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 17 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 18 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 19 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 20 | + |
| 21 | +INCREASE_COLOR = "#009E73" # Okabe-Ito position 1 (brand green) |
| 22 | +DECREASE_COLOR = "#D55E00" # Okabe-Ito position 2 (vermillion) |
| 23 | + |
14 | 24 | products = [ |
15 | 25 | "Product A", |
16 | 26 | "Product B", |
|
26 | 36 | q1_sales = [85, 72, 91, 45, 68, 53, 78, 62, 40, 88] |
27 | 37 | q4_sales = [92, 65, 88, 71, 74, 48, 95, 58, 67, 82] |
28 | 38 |
|
29 | | -# Determine direction for color coding |
30 | | -colors = [] |
31 | | -for start, end in zip(q1_sales, q4_sales, strict=True): |
32 | | - if end > start: |
33 | | - colors.append("#306998") # Python Blue - increase |
34 | | - elif end < start: |
35 | | - colors.append("#FFD43B") # Python Yellow - decrease |
36 | | - else: |
37 | | - colors.append("#888888") # Gray - no change |
38 | | - |
39 | | -# Create figure |
| 39 | +colors = [INCREASE_COLOR if end > start else DECREASE_COLOR for start, end in zip(q1_sales, q4_sales, strict=True)] |
| 40 | + |
| 41 | + |
| 42 | +def spread_labels(ys, min_gap=4.5): |
| 43 | + """Shift label y-positions apart so dense clusters don't overlap.""" |
| 44 | + n = len(ys) |
| 45 | + order = sorted(range(n), key=lambda i: ys[i]) |
| 46 | + adjusted = [float(ys[i]) for i in order] |
| 47 | + for _ in range(30): |
| 48 | + changed = False |
| 49 | + for i in range(1, n): |
| 50 | + if adjusted[i] - adjusted[i - 1] < min_gap: |
| 51 | + mid = (adjusted[i] + adjusted[i - 1]) / 2 |
| 52 | + adjusted[i - 1] = mid - min_gap / 2 |
| 53 | + adjusted[i] = mid + min_gap / 2 |
| 54 | + changed = True |
| 55 | + if not changed: |
| 56 | + break |
| 57 | + result = [0.0] * n |
| 58 | + for new_i, orig_i in enumerate(order): |
| 59 | + result[orig_i] = adjusted[new_i] |
| 60 | + return result |
| 61 | + |
| 62 | + |
| 63 | +left_y = spread_labels(q1_sales) |
| 64 | +right_y = spread_labels(q4_sales) |
| 65 | + |
40 | 66 | p = figure( |
41 | 67 | width=4800, |
42 | 68 | height=2700, |
43 | | - title="slope-basic · bokeh · pyplots.ai", |
| 69 | + title="slope-basic · bokeh · anyplot.ai", |
44 | 70 | x_range=(-0.5, 1.5), |
45 | | - y_range=(25, 105), |
| 71 | + y_range=(25, 112), |
46 | 72 | toolbar_location=None, |
47 | 73 | ) |
48 | 74 |
|
49 | | -# Style title and axes |
| 75 | +p.background_fill_color = PAGE_BG |
| 76 | +p.border_fill_color = PAGE_BG |
| 77 | +p.outline_line_color = None |
| 78 | + |
50 | 79 | p.title.text_font_size = "32pt" |
51 | 80 | p.title.align = "center" |
| 81 | +p.title.text_color = INK |
52 | 82 |
|
53 | | -# Remove x axis tick labels and set custom labels for time points |
54 | 83 | p.xaxis.visible = False |
55 | 84 | p.yaxis.axis_label = "Sales (thousands)" |
56 | 85 | p.yaxis.axis_label_text_font_size = "22pt" |
| 86 | +p.yaxis.axis_label_text_color = INK |
57 | 87 | p.yaxis.major_label_text_font_size = "18pt" |
| 88 | +p.yaxis.major_label_text_color = INK_SOFT |
| 89 | +p.yaxis.axis_line_color = INK_SOFT |
| 90 | +p.yaxis.major_tick_line_color = INK_SOFT |
| 91 | + |
| 92 | +p.xgrid.grid_line_color = None |
| 93 | +p.ygrid.grid_line_color = INK_SOFT |
| 94 | +p.ygrid.grid_line_alpha = 0.10 |
58 | 95 |
|
59 | | -# Add time point labels at bottom |
60 | | -p.add_layout(Label(x=0, y=32, text="Q1", text_font_size="28pt", text_align="center", text_baseline="top")) |
61 | | -p.add_layout(Label(x=1, y=32, text="Q4", text_font_size="28pt", text_align="center", text_baseline="top")) |
| 96 | +# Time point labels |
| 97 | +for x_pos, label in [(0, "Q1"), (1, "Q4")]: |
| 98 | + p.add_layout( |
| 99 | + Label( |
| 100 | + x=x_pos, y=27, text=label, text_font_size="28pt", text_align="center", text_baseline="top", text_color=INK |
| 101 | + ) |
| 102 | + ) |
| 103 | + |
| 104 | +# Direction legend in upper-center (above data range) |
| 105 | +for y_pos, legend_text, color in [(109, "— Increase", INCREASE_COLOR), (103, "— Decrease", DECREASE_COLOR)]: |
| 106 | + p.add_layout( |
| 107 | + Label( |
| 108 | + x=0.5, |
| 109 | + y=y_pos, |
| 110 | + text=legend_text, |
| 111 | + text_font_size="20pt", |
| 112 | + text_align="center", |
| 113 | + text_baseline="middle", |
| 114 | + text_color=color, |
| 115 | + ) |
| 116 | + ) |
62 | 117 |
|
63 | | -# Draw slope lines connecting Q1 to Q4 for each product |
| 118 | +# ColumnDataSource for scatter enables HoverTool |
| 119 | +scatter_data: dict[str, list] = {"x": [], "y": [], "color": [], "product": [], "period": [], "value": []} |
64 | 120 | for product, start, end, color in zip(products, q1_sales, q4_sales, colors, strict=True): |
65 | | - # Draw the connecting line |
66 | | - p.line(x=[0, 1], y=[start, end], line_width=4, line_color=color, line_alpha=0.8) |
| 121 | + scatter_data["x"].extend([0, 1]) |
| 122 | + scatter_data["y"].extend([start, end]) |
| 123 | + scatter_data["color"].extend([color, color]) |
| 124 | + scatter_data["product"].extend([product, product]) |
| 125 | + scatter_data["period"].extend(["Q1", "Q4"]) |
| 126 | + scatter_data["value"].extend([start, end]) |
67 | 127 |
|
68 | | - # Add markers at both endpoints |
69 | | - p.scatter(x=[0, 1], y=[start, end], size=18, color=color, alpha=0.9) |
| 128 | +source = ColumnDataSource(data=scatter_data) |
70 | 129 |
|
71 | | - # Add labels at start (Q1) - left aligned |
| 130 | +# Draw slope lines and endpoint labels |
| 131 | +for i, (product, start, end, color) in enumerate(zip(products, q1_sales, q4_sales, colors, strict=True)): |
| 132 | + p.line(x=[0, 1], y=[start, end], line_width=4, line_color=color, line_alpha=0.8) |
72 | 133 | p.add_layout( |
73 | 134 | Label( |
74 | 135 | x=-0.05, |
75 | | - y=start, |
| 136 | + y=left_y[i], |
76 | 137 | text=f"{product}: {start}", |
77 | 138 | text_font_size="18pt", |
78 | 139 | text_align="right", |
79 | 140 | text_baseline="middle", |
80 | 141 | text_color=color, |
81 | 142 | ) |
82 | 143 | ) |
83 | | - |
84 | | - # Add labels at end (Q4) - right aligned |
85 | 144 | p.add_layout( |
86 | 145 | Label( |
87 | 146 | x=1.05, |
88 | | - y=end, |
| 147 | + y=right_y[i], |
89 | 148 | text=f"{end}: {product}", |
90 | 149 | text_font_size="18pt", |
91 | 150 | text_align="left", |
|
94 | 153 | ) |
95 | 154 | ) |
96 | 155 |
|
97 | | -# Style grid |
98 | | -p.xgrid.grid_line_color = None |
99 | | -p.ygrid.grid_line_alpha = 0.3 |
100 | | -p.ygrid.grid_line_dash = "dashed" |
101 | | - |
102 | | -# Style outline |
103 | | -p.outline_line_color = None |
104 | | - |
105 | | -# Save as PNG |
106 | | -export_png(p, filename="plot.png") |
| 156 | +dots = p.scatter(x="x", y="y", size=18, color="color", source=source, alpha=0.9) |
| 157 | +p.add_tools( |
| 158 | + HoverTool( |
| 159 | + renderers=[dots], tooltips=[("Product", "@product"), ("Period", "@period"), ("Sales", "@value{0} thousand")] |
| 160 | + ) |
| 161 | +) |
107 | 162 |
|
108 | | -# Also save as HTML for interactive viewing |
109 | | -save(p, filename="plot.html", resources=CDN, title="slope-basic · bokeh · pyplots.ai") |
| 163 | +export_png(p, filename=f"plot-{THEME}.png") |
| 164 | +save(p, filename=f"plot-{THEME}.html", resources=CDN, title="slope-basic · bokeh · anyplot.ai") |
0 commit comments