Skip to content

Commit 54d669f

Browse files
feat(plotnine): implement phase-diagram (#3053)
## Implementation: `phase-diagram` - plotnine Implements the **plotnine** version of `phase-diagram`. **File:** `plots/phase-diagram/implementations/plotnine.py` **Parent Issue:** #3004 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20617670372)* --------- 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 fd2be57 commit 54d669f

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
""" pyplots.ai
2+
phase-diagram: Phase Diagram (State Space Plot)
3+
Library: plotnine 0.15.2 | Python 3.13.11
4+
Quality: 93/100 | Created: 2025-12-31
5+
"""
6+
7+
import numpy as np
8+
import pandas as pd
9+
from plotnine import (
10+
aes,
11+
element_line,
12+
element_text,
13+
geom_hline,
14+
geom_path,
15+
geom_point,
16+
geom_vline,
17+
ggplot,
18+
labs,
19+
scale_color_gradient,
20+
theme,
21+
theme_minimal,
22+
)
23+
24+
25+
# Data: Damped harmonic oscillator (spiral trajectory converging to equilibrium)
26+
np.random.seed(42)
27+
28+
# System parameters
29+
omega = 2 * np.pi # Natural frequency
30+
gamma = 0.15 # Damping coefficient
31+
32+
# Time array for smooth trajectory
33+
t = np.linspace(0, 8, 800)
34+
35+
# Solution for damped harmonic oscillator: x = A * exp(-gamma*t) * cos(omega*t)
36+
A = 2.0 # Initial amplitude
37+
x = A * np.exp(-gamma * t) * np.cos(omega * t)
38+
dx_dt = A * np.exp(-gamma * t) * (-gamma * np.cos(omega * t) - omega * np.sin(omega * t))
39+
40+
# Create DataFrame with time for color gradient
41+
df = pd.DataFrame({"x": x, "dx_dt": dx_dt, "t": t})
42+
43+
# Create phase diagram
44+
plot = (
45+
ggplot(df, aes(x="x", y="dx_dt", color="t"))
46+
+ geom_path(size=1.5, alpha=0.9)
47+
+ geom_point(data=df.iloc[[0]], size=5, color="#306998", show_legend=False) # Start point
48+
+ geom_point(data=df.iloc[[-1]], size=5, color="#FFD43B", shape="s", show_legend=False) # End point (equilibrium)
49+
+ geom_hline(yintercept=0, linetype="dashed", alpha=0.4, size=0.5)
50+
+ geom_vline(xintercept=0, linetype="dashed", alpha=0.4, size=0.5)
51+
+ scale_color_gradient(low="#306998", high="#FFD43B", name="Time (s)")
52+
+ labs(x="Position x", y="Velocity dx/dt", title="phase-diagram · plotnine · pyplots.ai")
53+
+ theme_minimal()
54+
+ theme(
55+
figure_size=(16, 9),
56+
text=element_text(size=14),
57+
axis_title=element_text(size=20),
58+
axis_text=element_text(size=16),
59+
plot_title=element_text(size=24),
60+
legend_text=element_text(size=14),
61+
legend_title=element_text(size=16),
62+
panel_grid_major=element_line(alpha=0.3),
63+
panel_grid_minor=element_line(alpha=0.15),
64+
)
65+
)
66+
67+
# Save
68+
plot.save("plot.png", dpi=300, verbose=False)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
library: plotnine
2+
specification_id: phase-diagram
3+
created: '2025-12-31T11:06:50Z'
4+
updated: '2025-12-31T11:16:03Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20617670372
7+
issue: 3004
8+
python_version: 3.13.11
9+
library_version: 0.15.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/phase-diagram/plotnine/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/phase-diagram/plotnine/plot_thumb.png
12+
preview_html: null
13+
quality_score: 93
14+
review:
15+
strengths:
16+
- Excellent visualization of damped harmonic oscillator phase portrait with clear
17+
spiral convergence to equilibrium
18+
- Effective use of color gradient to show time evolution (blue to yellow)
19+
- Well-marked start and end points with distinct markers (circle and square)
20+
- Reference lines at x=0 and dx/dt=0 help identify equilibrium and zero-crossing
21+
points
22+
- Clean, professional appearance with proper plotnine grammar of graphics implementation
23+
- Mathematically correct derivative calculation for damped oscillator
24+
weaknesses:
25+
- Axis labels could include physical units (e.g., Position x (m), Velocity dx/dt
26+
(m/s))
27+
- Only single trajectory shown; spec notes that multiple trajectories from different
28+
initial conditions could reveal basin of attraction structure

0 commit comments

Comments
 (0)