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
72 changes: 41 additions & 31 deletions plots/slope-basic/implementations/python/letsplot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" anyplot.ai
slope-basic: Basic Slope Chart (Slopegraph)
Library: letsplot 4.9.0 | Python 3.13.13
Quality: 85/100 | Updated: 2026-04-30
Library: letsplot 4.11.0 | Python 3.13.14
Quality: 94/100 | Updated: 2026-07-26
"""

import os
Expand All @@ -23,6 +23,7 @@
labs,
layer_tooltips,
scale_color_manual,
scale_linetype_manual,
scale_x_continuous,
scale_y_continuous,
theme,
Expand All @@ -40,26 +41,28 @@
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
GRID_COLOR = "#C8C7BF" if THEME == "light" else "#333330"

# Okabe-Ito: green = increase, orange = decrease
# Imprint palette: brand green = increase, matte red = decrease
COLOR_INCREASE = "#009E73"
COLOR_DECREASE = "#AE3030" # imprint red — decrease
COLOR_DECREASE = "#AE3030"

# Consumer electronics quarterly sales ($K): Q1 vs Q4
# Consumer electronics quarterly sales ($K): Q1 vs Q4.
# Values are spread with a minimum gap of ~20 at both endpoints so the
# entity + value labels never sit close enough to overlap.
data = {
"entity": [
"Streaming Devices",
"Gaming Consoles",
"Laptops",
"Smartphones",
"Tablets",
"Smart TVs",
"Monitors",
"Headphones",
"Cameras",
"Streaming Devices",
"Monitors",
"Speakers",
"Tablets",
"Gaming Consoles",
"Cameras",
"Smartphones",
],
"Q1": [80, 210, 120, 155, 140, 195, 55, 175, 105, 90],
"Q4": [130, 175, 160, 120, 185, 215, 95, 140, 150, 60],
"Q1": [50, 72, 95, 118, 140, 162, 184, 206, 228, 250],
"Q4": [55, 90, 140, 160, 222, 245, 115, 200, 270, 180],
}
df = pd.DataFrame(data)
df["change"] = df["Q4"] - df["Q1"]
Expand Down Expand Up @@ -89,7 +92,7 @@
{"entity": df["entity"].tolist() * 2, "value": df["Q1"].tolist() + df["Q4"].tolist(), "x": [0] * 10 + [1] * 10}
).merge(df[["entity", "direction"]], on="entity")

# Endpoint labels: entity name + value for legibility in crowded regions
# Endpoint labels: entity name + value for legibility
df_left = pd.DataFrame(
{
"entity": df["entity"].values,
Expand Down Expand Up @@ -128,12 +131,12 @@
# Background lines: dimmed to let top movers stand out
+ geom_segment(
data=seg_normal,
mapping=aes(x="x_start", y="y_start", xend="x_end", yend="y_end", color="direction"),
mapping=aes(x="x_start", y="y_start", xend="x_end", yend="y_end", color="direction", linetype="direction"),
size=1.8,
alpha=0.40,
alpha=0.55,
tooltips=_tooltip_normal,
)
# Top-mover lines: bold, fully opaque — emphasises biggest Q1→Q4 changes
# Top-mover lines: bold, fully opaque — emphasises the biggest Q1→Q4 changes
+ geom_segment(
data=seg_top,
mapping=aes(x="x_start", y="y_start", xend="x_end", yend="y_end", color="direction"),
Expand All @@ -142,33 +145,40 @@
tooltips=_tooltip_top,
)
+ geom_point(data=df_long, mapping=aes(x="x", y="value", color="direction"), size=6)
+ geom_text(data=df_left, mapping=aes(x="x", y="value", label="label"), hjust=1.08, size=10, color=INK_SOFT)
+ geom_text(data=df_right, mapping=aes(x="x", y="value", label="label"), hjust=-0.08, size=10, color=INK_SOFT)
+ geom_text(
data=df_left, mapping=aes(x="x", y="value", label="label"), hjust=1, nudge_x=-0.07, size=4.2, color=INK_SOFT
)
+ geom_text(
data=df_right, mapping=aes(x="x", y="value", label="label"), hjust=0, nudge_x=0.07, size=4.2, color=INK_SOFT
)
+ scale_color_manual(values={"Increase": COLOR_INCREASE, "Decrease": COLOR_DECREASE})
+ scale_linetype_manual(values={"Increase": "solid", "Decrease": "dashed"}, guide="none")
+ scale_x_continuous(breaks=[0, 1], labels=["Q1 Sales ($K)", "Q4 Sales ($K)"], limits=[-1.05, 2.05])
+ scale_y_continuous(limits=[25, 245])
+ labs(title="slope-basic · letsplot · anyplot.ai", x="", y="Sales ($K)", color="Change")
+ scale_y_continuous(limits=[20, 300])
+ labs(title="slope-basic · python · letsplot · anyplot.ai", x="", y="Sales ($K)", color="Change")
+ theme_minimal()
+ theme(
plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
panel_background=element_rect(fill=PAGE_BG),
# color matches fill so the rect draws no border (theme_minimal has no
# spines by default — the "remove all spines" clean-look alternative)
panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
panel_grid_major_y=element_line(color=GRID_COLOR, size=0.3),
panel_grid_major_x=element_blank(),
panel_grid_minor=element_blank(),
plot_title=element_text(size=28, color=INK),
axis_title_y=element_text(size=22, color=INK),
plot_title=element_text(size=16, color=INK),
axis_title_y=element_text(size=12, color=INK),
axis_title_x=element_blank(),
axis_text_x=element_text(size=20, color=INK_SOFT),
axis_text_y=element_text(size=18, color=INK_SOFT),
axis_text_x=element_text(size=12, color=INK_SOFT),
axis_text_y=element_text(size=10, color=INK_SOFT),
legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),
legend_title=element_text(size=20, color=INK),
legend_text=element_text(size=18, color=INK_SOFT),
legend_title=element_text(size=12, color=INK),
legend_text=element_text(size=10, color=INK_SOFT),
)
+ ggsize(1600, 900)
+ ggsize(800, 450)
)

# Save PNG (scale 3x → 4800 × 2700 px)
ggsave(plot, f"plot-{THEME}.png", path=".", scale=3)
# Save PNG: scale=4 on ggsize(800, 450) -> 3200 x 1800 px (canonical landscape canvas)
ggsave(plot, f"plot-{THEME}.png", path=".", scale=4)

# Save HTML for letsplot interactive tooltips
ggsave(plot, f"plot-{THEME}.html", path=".")
Loading
Loading