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
71 changes: 51 additions & 20 deletions plots/bar-grouped/implementations/python/plotnine.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
""" pyplots.ai
""" anyplot.ai
bar-grouped: Grouped Bar Chart
Library: plotnine 0.15.2 | Python 3.13.11
Quality: 91/100 | Created: 2025-12-24
Library: plotnine 0.15.4 | Python 3.13.13
Quality: 83/100 | Updated: 2026-05-06
"""

import os

import pandas as pd
from plotnine import aes, element_text, geom_bar, ggplot, labs, position_dodge, scale_fill_manual, theme, theme_minimal
from plotnine import (
aes,
element_line,
element_rect,
element_text,
geom_bar,
ggplot,
ggsave,
labs,
position_dodge,
scale_fill_manual,
theme,
)


# 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"

# Okabe-Ito palette (first series always #009E73)
OKABE_ITO = ["#009E73", "#D55E00", "#0072B2"]

# Data - Quarterly revenue by product line
data = {
Expand All @@ -29,26 +53,33 @@
}
df = pd.DataFrame(data)

# Define colors - Python Blue and Yellow, plus a complementary color
colors = ["#306998", "#FFD43B", "#4B8BBE"]
# Ensure categorical order matches Okabe-Ito palette order
df["Product"] = pd.Categorical(df["Product"], categories=["Software", "Hardware", "Services"], ordered=True)

# Theme-adaptive chrome
anyplot_theme = theme(
plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
panel_background=element_rect(fill=PAGE_BG),
panel_grid_major=element_line(color=INK_SOFT, size=0.3, alpha=0.10),
panel_border=element_rect(color=INK_SOFT, fill=None),
axis_title=element_text(size=20, color=INK),
axis_text=element_text(size=16, color=INK_SOFT),
axis_line=element_line(color=INK_SOFT),
plot_title=element_text(size=24, color=INK),
legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),
legend_text=element_text(size=16, color=INK_SOFT),
legend_title=element_text(size=18, color=INK),
figure_size=(16, 9),
)

# Create grouped bar chart
# Plot
plot = (
ggplot(df, aes(x="Quarter", y="Revenue", fill="Product"))
+ geom_bar(stat="identity", position=position_dodge(width=0.8), width=0.7)
+ scale_fill_manual(values=colors)
+ labs(x="Quarter", y="Revenue ($ millions)", title="bar-grouped · plotnine · pyplots.ai", fill="Product Line")
+ theme_minimal()
+ theme(
figure_size=(16, 9),
text=element_text(size=14),
axis_title=element_text(size=20),
axis_text=element_text(size=16),
plot_title=element_text(size=24),
legend_text=element_text(size=16),
legend_title=element_text(size=18),
)
+ scale_fill_manual(values=OKABE_ITO)
+ labs(x="Quarter", y="Revenue ($ millions)", title="bar-grouped · plotnine · anyplot.ai", fill="Product Line")
+ anyplot_theme
)

# Save
plot.save("plot.png", dpi=300)
ggsave(plot, filename=f"plot-{THEME}.png", dpi=300, width=16, height=9)
Loading
Loading