|
| 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) |
0 commit comments