Skip to content

Commit d19752c

Browse files
feat(plotly): implement line-interactive (#2803)
## Implementation: `line-interactive` - plotly Implements the **plotly** version of `line-interactive`. **File:** `plots/line-interactive/implementations/plotly.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20602450418)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 1808d86 commit d19752c

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
""" pyplots.ai
2+
line-interactive: Interactive Line Chart with Hover and Zoom
3+
Library: plotly 6.5.0 | Python 3.13.11
4+
Quality: 93/100 | Created: 2025-12-30
5+
"""
6+
7+
import numpy as np
8+
import pandas as pd
9+
import plotly.graph_objects as go
10+
11+
12+
# Data - Server CPU usage over 7 days (hourly readings)
13+
np.random.seed(42)
14+
n_points = 168 # 7 days * 24 hours
15+
16+
dates = pd.date_range("2024-01-01", periods=n_points, freq="h")
17+
18+
# Simulate realistic CPU usage pattern with daily cycles and some anomalies
19+
base = 35 # base CPU usage
20+
daily_pattern = 20 * np.sin(np.linspace(0, 7 * 2 * np.pi, n_points)) # daily cycle
21+
weekly_trend = np.linspace(0, 10, n_points) # slight upward trend
22+
noise = np.random.normal(0, 5, n_points)
23+
24+
# Add some random spikes (anomalies)
25+
spikes = np.zeros(n_points)
26+
spike_indices = [45, 92, 120, 155]
27+
for idx in spike_indices:
28+
spikes[idx] = np.random.uniform(20, 35)
29+
30+
cpu_usage = base + daily_pattern + weekly_trend + noise + spikes
31+
cpu_usage = np.clip(cpu_usage, 5, 100) # Keep within 5-100%
32+
33+
# Create figure with interactive features
34+
fig = go.Figure()
35+
36+
fig.add_trace(
37+
go.Scatter(
38+
x=dates,
39+
y=cpu_usage,
40+
mode="lines",
41+
name="CPU Usage",
42+
line={"color": "#306998", "width": 2.5},
43+
hovertemplate="<b>%{x|%Y-%m-%d %H:%M}</b><br>CPU Usage: %{y:.1f}%<extra></extra>",
44+
)
45+
)
46+
47+
# Layout with interactive features
48+
fig.update_layout(
49+
title={
50+
"text": "Server Metrics · line-interactive · plotly · pyplots.ai",
51+
"font": {"size": 28},
52+
"x": 0.5,
53+
"xanchor": "center",
54+
},
55+
xaxis={
56+
"title": {"text": "Date & Time", "font": {"size": 22}},
57+
"tickfont": {"size": 16},
58+
"rangeslider": {"visible": True, "thickness": 0.08},
59+
"rangeselector": {
60+
"buttons": [
61+
{"count": 1, "label": "1d", "step": "day", "stepmode": "backward"},
62+
{"count": 3, "label": "3d", "step": "day", "stepmode": "backward"},
63+
{"step": "all", "label": "All"},
64+
],
65+
"font": {"size": 14},
66+
"bgcolor": "#f0f0f0",
67+
"activecolor": "#FFD43B",
68+
},
69+
"showgrid": True,
70+
"gridwidth": 1,
71+
"gridcolor": "rgba(0,0,0,0.1)",
72+
},
73+
yaxis={
74+
"title": {"text": "CPU Usage (%)", "font": {"size": 22}},
75+
"tickfont": {"size": 16},
76+
"range": [0, 105],
77+
"showgrid": True,
78+
"gridwidth": 1,
79+
"gridcolor": "rgba(0,0,0,0.1)",
80+
},
81+
template="plotly_white",
82+
hovermode="x unified",
83+
dragmode="zoom",
84+
margin={"l": 80, "r": 40, "t": 100, "b": 80},
85+
)
86+
87+
# Add modebar buttons for interactivity
88+
fig.update_layout(
89+
modebar={
90+
"add": ["zoom", "pan", "zoomIn", "zoomOut", "resetScale"],
91+
"remove": ["lasso", "select"],
92+
"bgcolor": "rgba(255,255,255,0.8)",
93+
}
94+
)
95+
96+
# Save as PNG (static snapshot)
97+
fig.write_image("plot.png", width=1600, height=900, scale=3)
98+
99+
# Save as HTML (interactive version)
100+
fig.write_html("plot.html", include_plotlyjs=True, full_html=True)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library: plotly
2+
specification_id: line-interactive
3+
created: '2025-12-30T17:48:10Z'
4+
updated: '2025-12-30T17:55:50Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20602450418
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 6.5.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/line-interactive/plotly/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/line-interactive/plotly/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/line-interactive/plotly/plot.html
13+
quality_score: 93
14+
review:
15+
strengths:
16+
- Excellent use of Plotly interactive features (rangeslider, rangeselector, hover
17+
templates)
18+
- Well-crafted realistic data with daily cycles, trend, noise, and anomaly spikes
19+
- Perfect title format following pyplots conventions
20+
- Clean visual design with appropriate text sizes and layout
21+
- Good use of hovertemplate for formatted datetime and percentage display
22+
weaknesses:
23+
- Missing visible legend in the plot (trace has name but legend not shown)
24+
- Pandas import is unnecessary - could use numpy for date generation

0 commit comments

Comments
 (0)