|
1 | | -""" pyplots.ai |
| 1 | +"""pyplots.ai |
2 | 2 | area-basic: Basic Area Chart |
3 | | -Library: plotly 6.5.0 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-23 |
| 3 | +Library: plotly 6.5.2 | Python 3.14.2 |
| 4 | +Quality: /100 | Updated: 2026-02-11 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import numpy as np |
|
19 | 19 | weekly_pattern = 1000 * np.sin(np.arange(30) * 2 * np.pi / 7) |
20 | 20 | noise = np.random.randn(30) * 500 |
21 | 21 | visitors = base + trend + weekly_pattern + noise |
22 | | -visitors = np.maximum(visitors, 2000) # Ensure no negative values |
| 22 | +visitors = np.maximum(visitors, 2000).astype(int) |
23 | 23 |
|
24 | 24 | # Create figure |
25 | 25 | fig = go.Figure() |
|
30 | 30 | y=visitors, |
31 | 31 | mode="lines", |
32 | 32 | fill="tozeroy", |
33 | | - fillcolor="rgba(48, 105, 152, 0.4)", |
34 | | - line={"color": "#306998", "width": 3}, |
| 33 | + fillcolor="rgba(48, 105, 152, 0.35)", |
| 34 | + line={"color": "#306998", "width": 3, "shape": "spline"}, |
35 | 35 | name="Daily Visitors", |
| 36 | + hovertemplate="<b>%{x|%b %d, %Y}</b><br>Visitors: %{y:,}<extra></extra>", |
36 | 37 | ) |
37 | 38 | ) |
38 | 39 |
|
39 | | -# Layout with proper sizing for 4800x2700 px |
| 40 | +# Annotate peak traffic day |
| 41 | +peak_idx = int(np.argmax(visitors)) |
| 42 | +fig.add_annotation( |
| 43 | + x=dates[peak_idx], |
| 44 | + y=visitors[peak_idx], |
| 45 | + text=f"Peak: {visitors[peak_idx]:,}", |
| 46 | + showarrow=True, |
| 47 | + arrowhead=2, |
| 48 | + arrowsize=1.5, |
| 49 | + ax=0, |
| 50 | + ay=-40, |
| 51 | + font={"size": 16, "color": "#306998"}, |
| 52 | + bordercolor="#306998", |
| 53 | + borderwidth=1.5, |
| 54 | + borderpad=4, |
| 55 | + bgcolor="rgba(255, 255, 255, 0.85)", |
| 56 | +) |
| 57 | + |
| 58 | +# Layout |
40 | 59 | fig.update_layout( |
41 | 60 | title={ |
42 | 61 | "text": "Daily Website Visitors · area-basic · plotly · pyplots.ai", |
|
45 | 64 | "xanchor": "center", |
46 | 65 | }, |
47 | 66 | xaxis={ |
48 | | - "title": {"text": "Date", "font": {"size": 22}}, |
| 67 | + "title": {"text": "Date (January 2024)", "font": {"size": 22}}, |
49 | 68 | "tickfont": {"size": 18}, |
50 | 69 | "showgrid": True, |
51 | 70 | "gridwidth": 1, |
52 | | - "gridcolor": "rgba(0, 0, 0, 0.1)", |
| 71 | + "gridcolor": "rgba(0, 0, 0, 0.15)", |
| 72 | + "dtick": 7 * 24 * 60 * 60 * 1000, |
| 73 | + "tickformat": "%b %d", |
53 | 74 | }, |
54 | 75 | yaxis={ |
55 | 76 | "title": {"text": "Visitors (daily count)", "font": {"size": 22}}, |
56 | 77 | "tickfont": {"size": 18}, |
57 | 78 | "showgrid": True, |
58 | 79 | "gridwidth": 1, |
59 | | - "gridcolor": "rgba(0, 0, 0, 0.1)", |
| 80 | + "gridcolor": "rgba(0, 0, 0, 0.15)", |
| 81 | + "tickformat": ",", |
60 | 82 | }, |
61 | 83 | template="plotly_white", |
62 | 84 | showlegend=False, |
63 | 85 | margin={"l": 80, "r": 40, "t": 80, "b": 60}, |
| 86 | + hovermode="x unified", |
64 | 87 | ) |
65 | 88 |
|
66 | 89 | # Save as PNG (4800x2700 px) |
67 | 90 | fig.write_image("plot.png", width=1600, height=900, scale=3) |
68 | 91 |
|
69 | | -# Save interactive HTML |
| 92 | +# Save interactive HTML with range slider for exploration |
| 93 | +fig.update_layout(xaxis_rangeslider_visible=True) |
70 | 94 | fig.write_html("plot.html") |
0 commit comments