-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotly.py
More file actions
126 lines (112 loc) · 3.39 KB
/
plotly.py
File metadata and controls
126 lines (112 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
""" anyplot.ai
slope-basic: Basic Slope Chart (Slopegraph)
Library: plotly 6.7.0 | Python 3.13.13
Quality: 88/100 | Updated: 2026-04-30
"""
import os
import plotly.graph_objects as go
# Theme tokens
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F"
GRID = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)"
# Okabe-Ito: increase = brand green, decrease = vermillion, flat = adaptive neutral
COLOR_UP = "#009E73"
COLOR_DOWN = "#D55E00"
COLOR_FLAT = INK_MUTED
# Data - Product sales Q1 vs Q4 comparison (10 products showing various patterns)
products = [
"Laptop Pro",
"Wireless Earbuds",
"Smart Watch",
"Tablet Ultra",
"Gaming Mouse",
"Mechanical Keyboard",
"Webcam HD",
"USB Hub",
"Portable SSD",
"Monitor Stand",
]
sales_q1 = [245, 180, 120, 195, 85, 110, 45, 30, 75, 55]
sales_q4 = [310, 220, 195, 160, 145, 130, 95, 85, 70, 40]
colors = []
for q1, q4 in zip(sales_q1, sales_q4, strict=True):
if q4 > q1:
colors.append(COLOR_UP)
elif q4 < q1:
colors.append(COLOR_DOWN)
else:
colors.append(COLOR_FLAT)
# Plot
fig = go.Figure()
for i, product in enumerate(products):
fig.add_trace(
go.Scatter(
x=[0, 1],
y=[sales_q1[i], sales_q4[i]],
mode="lines+markers",
line={"color": colors[i], "width": 3},
marker={"size": 14, "color": colors[i]},
name=product,
showlegend=False,
hovertemplate=f"{product}<br>Q1: ${sales_q1[i]}K<br>Q4: ${sales_q4[i]}K<extra></extra>",
)
)
# Labels at Q1 (left side)
for i, product in enumerate(products):
fig.add_annotation(
x=-0.05,
y=sales_q1[i],
text=f"{product}: ${sales_q1[i]}K",
showarrow=False,
xanchor="right",
font={"size": 16, "color": colors[i]},
)
# Labels at Q4 (right side)
for i, product in enumerate(products):
fig.add_annotation(
x=1.05,
y=sales_q4[i],
text=f"${sales_q4[i]}K: {product}",
showarrow=False,
xanchor="left",
font={"size": 16, "color": colors[i]},
)
# Style
fig.update_layout(
paper_bgcolor=PAGE_BG,
plot_bgcolor=PAGE_BG,
font={"color": INK},
title={
"text": "Product Sales Q1 vs Q4 · slope-basic · plotly · anyplot.ai",
"font": {"size": 28, "color": INK},
"x": 0.5,
"xanchor": "center",
},
xaxis={
"tickmode": "array",
"tickvals": [0, 1],
"ticktext": ["Q1 2024", "Q4 2024"],
"tickfont": {"size": 22, "color": INK_SOFT},
"range": [-0.5, 1.5],
"showgrid": False,
"zeroline": False,
"linecolor": INK_SOFT,
},
yaxis={
"title": {"text": "Sales ($K)", "font": {"size": 22, "color": INK}},
"tickfont": {"size": 18, "color": INK_SOFT},
"showgrid": True,
"gridwidth": 1,
"gridcolor": GRID,
"zeroline": False,
"linecolor": INK_SOFT,
},
margin={"l": 220, "r": 220, "t": 80, "b": 60},
)
# Save
fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3)
fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn")