|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | lollipop-basic: Basic Lollipop Chart |
3 | | -Library: seaborn 0.13.2 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-23 |
| 3 | +Library: seaborn 0.13.2 | Python 3.14.4 |
| 4 | +Quality: 87/100 | Updated: 2026-04-26 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import matplotlib.pyplot as plt |
8 | 10 | import pandas as pd |
9 | 11 | import seaborn as sns |
10 | 12 |
|
11 | 13 |
|
12 | | -# Data - Product sales by category, sorted by value |
| 14 | +# Theme tokens |
| 15 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 16 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 17 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 18 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 19 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 20 | +BRAND = "#009E73" # Okabe-Ito position 1 |
| 21 | + |
| 22 | +sns.set_theme( |
| 23 | + style="ticks", |
| 24 | + rc={ |
| 25 | + "figure.facecolor": PAGE_BG, |
| 26 | + "axes.facecolor": PAGE_BG, |
| 27 | + "axes.edgecolor": INK_SOFT, |
| 28 | + "axes.labelcolor": INK, |
| 29 | + "text.color": INK, |
| 30 | + "xtick.color": INK_SOFT, |
| 31 | + "ytick.color": INK_SOFT, |
| 32 | + "grid.color": INK, |
| 33 | + "grid.alpha": 0.10, |
| 34 | + "legend.facecolor": ELEVATED_BG, |
| 35 | + "legend.edgecolor": INK_SOFT, |
| 36 | + }, |
| 37 | +) |
| 38 | + |
| 39 | +# Data - Product sales by category |
13 | 40 | categories = [ |
14 | 41 | "Electronics", |
15 | 42 | "Clothing", |
|
24 | 51 | ] |
25 | 52 | values = [85000, 72000, 58000, 45000, 42000, 38000, 35000, 28000, 25000, 18000] |
26 | 53 |
|
27 | | -# Create DataFrame and sort by value (ascending for horizontal lollipop) |
28 | | -df = pd.DataFrame({"category": categories, "value": values}) |
29 | | -df = df.sort_values("value", ascending=True) |
| 54 | +df = pd.DataFrame({"category": categories, "value": values}).sort_values("value", ascending=True) |
30 | 55 |
|
31 | 56 | # Plot |
32 | | -fig, ax = plt.subplots(figsize=(16, 9)) |
| 57 | +fig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG) |
| 58 | +ax.set_facecolor(PAGE_BG) |
33 | 59 |
|
34 | | -# Draw stems (thin lines from baseline to marker) |
35 | | -ax.hlines(y=df["category"], xmin=0, xmax=df["value"], color="#306998", linewidth=2.5, alpha=0.8) |
| 60 | +# Stems: thin lines from baseline to marker |
| 61 | +ax.hlines(y=df["category"], xmin=0, xmax=df["value"], color=BRAND, linewidth=2.5, alpha=0.85, zorder=2) |
36 | 62 |
|
37 | | -# Draw markers (circular dots at data values) using seaborn |
| 63 | +# Markers: circular dots at the data values |
38 | 64 | sns.scatterplot( |
39 | | - data=df, x="value", y="category", s=400, color="#FFD43B", edgecolor="#306998", linewidth=2, ax=ax, zorder=3 |
| 65 | + data=df, x="value", y="category", s=450, color=BRAND, edgecolor=PAGE_BG, linewidth=2, ax=ax, zorder=3, legend=False |
40 | 66 | ) |
41 | 67 |
|
42 | | -# Styling |
43 | | -ax.set_xlabel("Sales ($)", fontsize=20) |
44 | | -ax.set_ylabel("Product Category", fontsize=20) |
45 | | -ax.set_title("lollipop-basic · seaborn · pyplots.ai", fontsize=24) |
46 | | -ax.tick_params(axis="both", labelsize=16) |
47 | | -ax.set_xlim(0, None) |
48 | | -ax.grid(True, axis="x", alpha=0.3, linestyle="--") |
| 68 | +# Style |
| 69 | +ax.set_xlabel("Sales ($)", fontsize=20, color=INK) |
| 70 | +ax.set_ylabel("Product Category", fontsize=20, color=INK) |
| 71 | +ax.set_title("lollipop-basic · seaborn · anyplot.ai", fontsize=24, fontweight="medium", color=INK, pad=18) |
| 72 | +ax.tick_params(axis="both", labelsize=16, colors=INK_SOFT) |
| 73 | +ax.set_xlim(0, max(values) * 1.08) |
| 74 | + |
| 75 | +ax.xaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK) |
| 76 | +ax.yaxis.grid(False) |
| 77 | +ax.set_axisbelow(True) |
49 | 78 |
|
50 | | -# Remove top and right spines for cleaner look |
51 | 79 | ax.spines["top"].set_visible(False) |
52 | 80 | ax.spines["right"].set_visible(False) |
| 81 | +for s in ("left", "bottom"): |
| 82 | + ax.spines[s].set_color(INK_SOFT) |
53 | 83 |
|
54 | 84 | plt.tight_layout() |
55 | | -plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
| 85 | +plt.savefig(f"plot-{THEME}.png", dpi=300, bbox_inches="tight", facecolor=PAGE_BG) |
0 commit comments