Skip to content

Commit b1182e2

Browse files
feat(seaborn): implement andrews-curves (#2898)
## Implementation: `andrews-curves` - seaborn Implements the **seaborn** version of `andrews-curves`. **File:** `plots/andrews-curves/implementations/seaborn.py` **Parent Issue:** #2859 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20608490654)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent ab6804c commit b1182e2

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
""" pyplots.ai
2+
andrews-curves: Andrews Curves for Multivariate Data
3+
Library: seaborn 0.13.2 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-30
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
import pandas as pd
10+
import seaborn as sns
11+
12+
13+
# Load iris dataset from seaborn
14+
df = sns.load_dataset("iris")
15+
16+
# Normalize variables to similar scales
17+
features = ["sepal_length", "sepal_width", "petal_length", "petal_width"]
18+
for col in features:
19+
df[col + "_norm"] = (df[col] - df[col].mean()) / df[col].std()
20+
21+
norm_features = [f + "_norm" for f in features]
22+
23+
# Generate t values from -pi to pi
24+
t = np.linspace(-np.pi, np.pi, 200)
25+
26+
# Compute Andrews curves for all observations
27+
# Andrews curve: f(t) = x1/sqrt(2) + x2*sin(t) + x3*cos(t) + x4*sin(2t) + ...
28+
curves_data = []
29+
for idx, row in df.iterrows():
30+
values = row[norm_features].values.astype(float)
31+
# Compute Fourier series for this observation
32+
curve_vals = np.full_like(t, values[0] / np.sqrt(2))
33+
for i in range(1, len(values)):
34+
if i % 2 == 1:
35+
curve_vals = curve_vals + values[i] * np.sin((i + 1) // 2 * t)
36+
else:
37+
curve_vals = curve_vals + values[i] * np.cos(i // 2 * t)
38+
39+
for t_val, y_val in zip(t, curve_vals, strict=True):
40+
curves_data.append({"t": t_val, "f(t)": y_val, "species": row["species"], "obs_id": idx})
41+
42+
curves_df = pd.DataFrame(curves_data)
43+
44+
# Color palette using Python Blue, Yellow, and a third color
45+
colors = {"setosa": "#306998", "versicolor": "#FFD43B", "virginica": "#E74C3C"}
46+
47+
# Create plot
48+
fig, ax = plt.subplots(figsize=(16, 9))
49+
sns.set_style("whitegrid")
50+
51+
# Plot Andrews curves using lineplot with grouped data
52+
sns.lineplot(
53+
data=curves_df,
54+
x="t",
55+
y="f(t)",
56+
hue="species",
57+
palette=colors,
58+
alpha=0.4,
59+
linewidth=1.5,
60+
units="obs_id",
61+
estimator=None,
62+
ax=ax,
63+
)
64+
65+
# Style the plot
66+
ax.set_xlabel("t", fontsize=20)
67+
ax.set_ylabel("f(t)", fontsize=20)
68+
ax.set_title("andrews-curves · seaborn · pyplots.ai", fontsize=24)
69+
ax.tick_params(axis="both", labelsize=16)
70+
71+
# Set x-axis ticks to show pi values
72+
ax.set_xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi])
73+
ax.set_xticklabels(["-π", "-π/2", "0", "π/2", "π"], fontsize=16)
74+
75+
# Customize legend
76+
ax.legend(title="Species", fontsize=16, title_fontsize=18, loc="upper right")
77+
78+
# Subtle grid
79+
ax.grid(True, alpha=0.3, linestyle="--")
80+
81+
plt.tight_layout()
82+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library: seaborn
2+
specification_id: andrews-curves
3+
created: '2025-12-30T23:57:52Z'
4+
updated: '2025-12-31T00:05:28Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20608490654
7+
issue: 2859
8+
python_version: 3.13.11
9+
library_version: 0.13.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/andrews-curves/seaborn/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/andrews-curves/seaborn/plot_thumb.png
12+
preview_html: null
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent implementation of Andrews curves Fourier transformation algorithm
17+
- Perfect use of seaborn lineplot with units parameter to plot individual curves
18+
without aggregation
19+
- Good choice of colorblind-safe color palette with appropriate transparency
20+
- Clean separation of species clusters visible in the visualization
21+
- Correct mathematical notation for x-axis labels using π symbols
22+
- Well-structured code following KISS principles
23+
weaknesses:
24+
- Axis labels use mathematical notation only (t, f(t)) without descriptive context
25+
for newcomers
26+
- Legend could be positioned to avoid any potential overlap with curve endpoints

0 commit comments

Comments
 (0)