-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotly.py
More file actions
95 lines (85 loc) · 2.55 KB
/
plotly.py
File metadata and controls
95 lines (85 loc) · 2.55 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
""" pyplots.ai
bar-basic: Basic Bar Chart
Library: plotly 6.5.2 | Python 3.14
Quality: 92/100 | Created: 2026-02-14
"""
import plotly.graph_objects as go
# Data — product sales by department, sorted descending
categories = ["Electronics", "Clothing", "Home & Garden", "Sports", "Books", "Toys", "Automotive", "Health"]
values = [45200, 38700, 31500, 27800, 24300, 21600, 18900, 15400]
# Highlight the top performer with a distinct shade
bar_colors = ["#1A4971"] + ["#306998"] * (len(categories) - 1)
# Create figure
fig = go.Figure()
fig.add_trace(
go.Bar(
x=categories,
y=values,
marker={"color": bar_colors, "line": {"color": "rgba(0,0,0,0.08)", "width": 1}},
text=values,
textposition="outside",
texttemplate="$%{text:,.0f}",
textfont={"size": 20, "color": "#444444"},
hovertemplate="<b>%{x}</b><br>Sales: $%{y:,.0f}<extra></extra>",
)
)
# Annotation: highlight the leading category with an insight callout
fig.add_annotation(
x="Electronics",
y=45200,
text="<b>Top seller</b><br>17% ahead of #2",
showarrow=True,
arrowhead=2,
arrowsize=1.2,
arrowwidth=2,
arrowcolor="#1A4971",
ax=100,
ay=-75,
font={"size": 18, "color": "#1A4971"},
align="left",
bordercolor="#1A4971",
borderwidth=1.5,
borderpad=6,
bgcolor="rgba(255,255,255,0.85)",
)
# Subtle average reference line
avg_value = sum(values) / len(values)
fig.add_hline(
y=avg_value,
line={"color": "rgba(0,0,0,0.25)", "width": 1.5, "dash": "dot"},
annotation={
"text": f"Avg ${avg_value:,.0f}",
"font": {"size": 16, "color": "#666666"},
"showarrow": False,
"xanchor": "left",
},
)
# Layout
fig.update_layout(
title={
"text": "bar-basic · plotly · pyplots.ai",
"font": {"size": 28, "color": "#222222"},
"x": 0.5,
"xanchor": "center",
},
xaxis={
"title": {"text": "Product Category", "font": {"size": 22, "color": "#333333"}},
"tickfont": {"size": 18, "color": "#444444"},
},
yaxis={
"title": {"text": "Sales ($)", "font": {"size": 22, "color": "#333333"}},
"tickfont": {"size": 18, "color": "#444444"},
"tickprefix": "$",
"tickformat": ",.0f",
"gridcolor": "rgba(0,0,0,0.07)",
"zeroline": False,
},
template="plotly_white",
bargap=0.3,
margin={"t": 100, "b": 80, "l": 100, "r": 120},
plot_bgcolor="white",
showlegend=False,
)
# Save
fig.write_image("plot.png", width=1600, height=900, scale=3)
fig.write_html("plot.html")