|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | slope-basic: Basic Slope Chart (Slopegraph) |
3 | | -Library: seaborn 0.13.2 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: seaborn 0.13.2 | Python 3.13.13 |
| 4 | +Quality: 83/100 | Updated: 2026-04-30 |
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 | from matplotlib.lines import Line2D |
11 | 13 |
|
12 | 14 |
|
13 | | -# Data - Tech company revenue growth comparison Q1 vs Q4 |
14 | | -# Values spaced to avoid label overlap |
| 15 | +# Theme tokens |
| 16 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 17 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 18 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 19 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 20 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 21 | + |
| 22 | +COLOR_INCREASE = "#009E73" # Okabe-Ito position 1 |
| 23 | +COLOR_DECREASE = "#D55E00" # Okabe-Ito position 2 |
| 24 | + |
| 25 | +sns.set_theme( |
| 26 | + style="ticks", |
| 27 | + rc={ |
| 28 | + "figure.facecolor": PAGE_BG, |
| 29 | + "axes.facecolor": PAGE_BG, |
| 30 | + "axes.edgecolor": INK_SOFT, |
| 31 | + "axes.labelcolor": INK, |
| 32 | + "text.color": INK, |
| 33 | + "xtick.color": INK_SOFT, |
| 34 | + "ytick.color": INK_SOFT, |
| 35 | + "grid.color": INK, |
| 36 | + "grid.alpha": 0.10, |
| 37 | + "legend.facecolor": ELEVATED_BG, |
| 38 | + "legend.edgecolor": INK_SOFT, |
| 39 | + }, |
| 40 | +) |
| 41 | + |
| 42 | +# Data — tech company revenue comparison Q1 vs Q4 (four rank crossings) |
15 | 43 | data = { |
16 | | - "entity": ["TechCorp", "DataFlow", "CloudNine", "NetWorks", "CodeBase", "ByteSize", "LogicPro", "SoftEdge"], |
17 | | - "Q1 Revenue ($M)": [145, 72, 215, 52, 178, 98, 125, 162], |
18 | | - "Q4 Revenue ($M)": [192, 108, 178, 72, 225, 82, 155, 138], |
| 44 | + "entity": ["StreamPeak", "DataCore", "CloudSync", "NetPulse", "CodeBase", "ByteFlow", "LogicGrid", "TechVault"], |
| 45 | + "Q1 ($M)": [50, 110, 165, 220, 275, 325, 378, 430], |
| 46 | + "Q4 ($M)": [95, 60, 230, 178, 335, 268, 415, 368], |
19 | 47 | } |
20 | 48 |
|
21 | 49 | df = pd.DataFrame(data) |
22 | | - |
23 | | -# Calculate change for color coding |
24 | | -df["change"] = df["Q4 Revenue ($M)"] - df["Q1 Revenue ($M)"] |
| 50 | +df["change"] = df["Q4 ($M)"] - df["Q1 ($M)"] |
25 | 51 | df["direction"] = df["change"].apply(lambda x: "Increase" if x > 0 else "Decrease") |
| 52 | +df = df.sort_values("Q1 ($M)").reset_index(drop=True) |
26 | 53 |
|
27 | | -# Sort by Q1 value for better visual layout |
28 | | -df = df.sort_values("Q1 Revenue ($M)").reset_index(drop=True) |
29 | | - |
30 | | -# Create figure |
31 | | -fig, ax = plt.subplots(figsize=(16, 9)) |
32 | | -sns.set_style("whitegrid") |
33 | | - |
34 | | -# Define colors - Python palette |
35 | | -colors = {"Increase": "#306998", "Decrease": "#FFD43B"} |
36 | | - |
37 | | -# Reshape data for seaborn |
38 | 54 | df_melted = df.melt( |
39 | | - id_vars=["entity", "direction"], |
40 | | - value_vars=["Q1 Revenue ($M)", "Q4 Revenue ($M)"], |
41 | | - var_name="Period", |
42 | | - value_name="Revenue", |
| 55 | + id_vars=["entity", "direction"], value_vars=["Q1 ($M)", "Q4 ($M)"], var_name="Period", value_name="Revenue ($M)" |
| 56 | +) |
| 57 | +df_melted["period_num"] = df_melted["Period"].map({"Q1 ($M)": 0, "Q4 ($M)": 1}) |
| 58 | + |
| 59 | +# Plot |
| 60 | +fig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG) |
| 61 | +ax.set_facecolor(PAGE_BG) |
| 62 | + |
| 63 | +palette = {"Increase": COLOR_INCREASE, "Decrease": COLOR_DECREASE} |
| 64 | + |
| 65 | +sns.lineplot( |
| 66 | + data=df_melted, |
| 67 | + x="period_num", |
| 68 | + y="Revenue ($M)", |
| 69 | + hue="direction", |
| 70 | + units="entity", |
| 71 | + estimator=None, |
| 72 | + palette=palette, |
| 73 | + linewidth=3.5, |
| 74 | + marker="o", |
| 75 | + markersize=12, |
| 76 | + alpha=0.85, |
| 77 | + legend=False, |
| 78 | + ax=ax, |
43 | 79 | ) |
44 | 80 |
|
45 | | -# Plot lines for each entity using seaborn |
46 | | -for entity in df["entity"]: |
47 | | - entity_data = df_melted[df_melted["entity"] == entity] |
48 | | - direction = entity_data["direction"].iloc[0] |
49 | | - sns.lineplot( |
50 | | - data=entity_data, |
51 | | - x="Period", |
52 | | - y="Revenue", |
53 | | - color=colors[direction], |
54 | | - linewidth=3.5, |
55 | | - alpha=0.85, |
56 | | - marker="o", |
57 | | - markersize=14, |
58 | | - ax=ax, |
59 | | - ) |
60 | | - |
61 | | -# Add entity labels at both ends |
62 | | -for _idx, row in df.iterrows(): |
63 | | - color = colors[row["direction"]] |
64 | | - q1_val = row["Q1 Revenue ($M)"] |
65 | | - q4_val = row["Q4 Revenue ($M)"] |
| 81 | +# Endpoint labels at both axes |
| 82 | +for _, row in df.iterrows(): |
| 83 | + color = palette[row["direction"]] |
| 84 | + q1_val = int(row["Q1 ($M)"]) |
| 85 | + q4_val = int(row["Q4 ($M)"]) |
66 | 86 |
|
67 | | - # Left labels (Q1) |
68 | 87 | ax.annotate( |
69 | 88 | f"{row['entity']} ({q1_val})", |
70 | 89 | xy=(0, q1_val), |
71 | | - xytext=(-12, 0), |
| 90 | + xytext=(-14, 0), |
72 | 91 | textcoords="offset points", |
73 | | - fontsize=15, |
| 92 | + fontsize=14, |
74 | 93 | color=color, |
75 | 94 | ha="right", |
76 | 95 | va="center", |
77 | 96 | fontweight="bold", |
78 | 97 | ) |
79 | | - # Right labels (Q4) |
80 | 98 | ax.annotate( |
81 | 99 | f"({q4_val}) {row['entity']}", |
82 | 100 | xy=(1, q4_val), |
83 | | - xytext=(12, 0), |
| 101 | + xytext=(14, 0), |
84 | 102 | textcoords="offset points", |
85 | | - fontsize=15, |
| 103 | + fontsize=14, |
86 | 104 | color=color, |
87 | 105 | ha="left", |
88 | 106 | va="center", |
89 | 107 | fontweight="bold", |
90 | 108 | ) |
91 | 109 |
|
92 | | -# Style adjustments |
| 110 | +# Style |
| 111 | +ax.set_xticks([0, 1]) |
| 112 | +ax.set_xticklabels(["Q1 Revenue ($M)", "Q4 Revenue ($M)"], fontsize=16, color=INK_SOFT) |
93 | 113 | ax.set_xlabel("") |
94 | | -ax.set_ylabel("Revenue ($M)", fontsize=20) |
95 | | -ax.set_title("slope-basic · seaborn · pyplots.ai", fontsize=24, fontweight="bold", pad=20) |
96 | | -ax.tick_params(axis="both", labelsize=16) |
| 114 | +ax.set_ylabel("Revenue ($M)", fontsize=20, color=INK) |
| 115 | +ax.set_title("slope-basic · seaborn · anyplot.ai", fontsize=24, fontweight="bold", pad=20, color=INK) |
| 116 | +ax.tick_params(axis="both", labelsize=16, colors=INK_SOFT) |
97 | 117 | ax.set_xlim(-0.15, 1.15) |
98 | 118 |
|
99 | | -# Add padding to y-axis for labels |
100 | | -y_min = min(df["Q1 Revenue ($M)"].min(), df["Q4 Revenue ($M)"].min()) |
101 | | -y_max = max(df["Q1 Revenue ($M)"].max(), df["Q4 Revenue ($M)"].max()) |
102 | | -y_padding = (y_max - y_min) * 0.08 |
| 119 | +y_min = min(df["Q1 ($M)"].min(), df["Q4 ($M)"].min()) |
| 120 | +y_max = max(df["Q1 ($M)"].max(), df["Q4 ($M)"].max()) |
| 121 | +y_padding = (y_max - y_min) * 0.10 |
103 | 122 | ax.set_ylim(y_min - y_padding, y_max + y_padding) |
104 | 123 |
|
105 | | -ax.grid(True, alpha=0.3, linestyle="--") |
| 124 | +ax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK) |
| 125 | +ax.spines["top"].set_visible(False) |
| 126 | +ax.spines["right"].set_visible(False) |
| 127 | +for spine in ("left", "bottom"): |
| 128 | + ax.spines[spine].set_color(INK_SOFT) |
106 | 129 |
|
107 | | -# Add legend for direction |
| 130 | +# Legend |
108 | 131 | legend_elements = [ |
109 | | - Line2D([0], [0], color="#306998", linewidth=3.5, marker="o", markersize=10, label="Increase"), |
110 | | - Line2D([0], [0], color="#FFD43B", linewidth=3.5, marker="o", markersize=10, label="Decrease"), |
| 132 | + Line2D([0], [0], color=COLOR_INCREASE, linewidth=3.5, marker="o", markersize=10, label="Increase"), |
| 133 | + Line2D([0], [0], color=COLOR_DECREASE, linewidth=3.5, marker="o", markersize=10, label="Decrease"), |
111 | 134 | ] |
112 | | -ax.legend(handles=legend_elements, loc="lower right", fontsize=16, framealpha=0.9) |
| 135 | +ax.legend( |
| 136 | + handles=legend_elements, loc="lower right", fontsize=16, framealpha=0.9, facecolor=ELEVATED_BG, edgecolor=INK_SOFT |
| 137 | +) |
113 | 138 |
|
114 | 139 | plt.tight_layout() |
115 | 140 | plt.subplots_adjust(left=0.22, right=0.78) |
116 | | -plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
| 141 | +plt.savefig(f"plot-{THEME}.png", dpi=300, bbox_inches="tight", facecolor=PAGE_BG) |
0 commit comments