Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 58 additions & 36 deletions plots/bar-basic/implementations/python/plotly.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
""" pyplots.ai
""" anyplot.ai
bar-basic: Basic Bar Chart
Library: plotly 6.5.2 | Python 3.14
Quality: 92/100 | Created: 2026-02-14
Library: plotly 6.7.0 | Python 3.13.13
Quality: 90/100 | Created: 2026-05-28
"""

import os
import sys


# Prevent self-import: remove this script's own directory from sys.path so that
# "import plotly" resolves to the installed package, not this file.
_here = os.path.dirname(os.path.abspath(__file__))
sys.path = [p for p in sys.path if os.path.abspath(p) != _here]

import plotly.graph_objects as go


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.15)" if THEME == "light" else "rgba(240,239,232,0.15)"

# anyplot palette: brand green for top performer, blue for rest
BRAND = "#009E73"
BASE = "#4467A3"

# 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)
bar_colors = [BRAND] + [BASE] * (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}},
marker={"color": bar_colors, "line": {"color": "rgba(0,0,0,0.06)", "width": 1}},
text=values,
textposition="outside",
texttemplate="$%{text:,.0f}",
textfont={"size": 20, "color": "#444444"},
textfont={"size": 10, "color": INK_SOFT},
hovertemplate="<b>%{x}</b><br>Sales: $%{y:,.0f}<extra></extra>",
)
)

# Annotation: highlight the leading category with an insight callout
# Annotation: arrow from Electronics bar top sweeps right to a callout in clear whitespace;
# the shallow diagonal passes above all shorter bars, avoiding the Clothing value label
fig.add_annotation(
x="Electronics",
y=45200,
Expand All @@ -39,57 +60,58 @@
arrowhead=2,
arrowsize=1.2,
arrowwidth=2,
arrowcolor="#1A4971",
ax=100,
ay=-75,
font={"size": 18, "color": "#1A4971"},
arrowcolor=BRAND,
ax=280,
ay=30,
font={"size": 10, "color": BRAND},
align="left",
bordercolor="#1A4971",
bordercolor=BRAND,
borderwidth=1.5,
borderpad=6,
bgcolor="rgba(255,255,255,0.85)",
bgcolor=ELEVATED_BG,
)

# 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",
},
line={"color": INK_MUTED, "width": 1.5, "dash": "dot"},
annotation_text=f"Avg ${avg_value:,.0f}",
annotation_font_size=10,
annotation_font_color=INK_MUTED,
annotation_position="top left",
)

# Layout
fig.update_layout(
autosize=False,
title={
"text": "bar-basic · plotly · pyplots.ai",
"font": {"size": 28, "color": "#222222"},
"text": "bar-basic · python · plotly · anyplot.ai",
"font": {"size": 16, "color": INK},
"x": 0.5,
"xanchor": "center",
},
xaxis={
"title": {"text": "Product Category", "font": {"size": 22, "color": "#333333"}},
"tickfont": {"size": 18, "color": "#444444"},
"title": {"text": "Product Category", "font": {"size": 12, "color": INK}},
"tickfont": {"size": 10, "color": INK_SOFT},
"linecolor": INK_SOFT,
"showgrid": False,
},
yaxis={
"title": {"text": "Sales ($)", "font": {"size": 22, "color": "#333333"}},
"tickfont": {"size": 18, "color": "#444444"},
"title": {"text": "Sales ($)", "font": {"size": 12, "color": INK}},
"tickfont": {"size": 10, "color": INK_SOFT},
"tickprefix": "$",
"tickformat": ",.0f",
"gridcolor": "rgba(0,0,0,0.07)",
"gridcolor": GRID,
"linecolor": INK_SOFT,
"zeroline": False,
},
paper_bgcolor=PAGE_BG,
plot_bgcolor=PAGE_BG,
font={"color": INK},
template="plotly_white",
bargap=0.3,
margin={"t": 100, "b": 80, "l": 100, "r": 120},
plot_bgcolor="white",
margin={"t": 80, "b": 60, "l": 100, "r": 40},
showlegend=False,
)

# Save
fig.write_image("plot.png", width=1600, height=900, scale=3)
fig.write_html("plot.html")
fig.write_image(f"plot-{THEME}.png", width=800, height=450, scale=4)
fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn")
Loading
Loading