|
1 | | -""" pyplots.ai |
| 1 | +"""pyplots.ai |
2 | 2 | area-basic: Basic Area Chart |
3 | 3 | Library: matplotlib 3.10.8 | Python 3.14.2 |
4 | | -Quality: 95/100 | Created: 2025-12-23 |
| 4 | +Quality: /100 | Updated: 2026-02-11 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import matplotlib.colors as mcolors |
|
11 | 11 | from matplotlib.path import Path |
12 | 12 |
|
13 | 13 |
|
14 | | -# Data - daily website visitors over a month with weekend dips |
| 14 | +# Data - daily website visitors over a month with weekend dips and a viral spike |
15 | 15 | np.random.seed(42) |
16 | 16 | days = np.arange(1, 31) |
17 | | -base_visitors = 5000 + np.linspace(0, 2500, 30) # Upward trend |
| 17 | +base_visitors = 3000 + np.linspace(0, 2000, 30) # Upward trend from 3k to 5k |
18 | 18 | weekend_effect = np.array([-1200 if d % 7 in (0, 6) else 0 for d in days]) # Weekend dips |
19 | | -noise = np.random.randn(30) * 400 |
| 19 | +noise = np.random.randn(30) * 300 |
20 | 20 | visitors = base_visitors + weekend_effect + noise |
21 | | -visitors = np.clip(visitors, 2000, 10000) |
| 21 | +# Viral blog post spike on day 18 |
| 22 | +visitors[17] = 8200 |
| 23 | +visitors[18] = 6800 |
| 24 | +visitors = np.clip(visitors, 1000, 10000) |
22 | 25 |
|
23 | 26 | # Create plot (4800x2700 px) |
24 | 27 | fig, ax = plt.subplots(figsize=(16, 9)) |
25 | 28 |
|
26 | | -y_max = visitors.max() * 1.15 |
| 29 | +y_max = visitors.max() * 1.12 |
27 | 30 |
|
28 | 31 | # Gradient fill using imshow clipped to the area shape |
29 | 32 | cmap = mcolors.LinearSegmentedColormap.from_list("area_grad", ["#d6e6f5", "#306998"]) |
30 | 33 | gradient = np.linspace(0, 1, 256).reshape(-1, 1) |
31 | 34 | gradient = np.hstack([gradient, gradient]) |
32 | 35 |
|
33 | | -# Build clip path manually from fill_between polygon |
| 36 | +# Build clip path from area polygon |
34 | 37 | verts = [(days[0], 0)] |
35 | 38 | for d, v in zip(days, visitors, strict=True): |
36 | 39 | verts.append((d, v)) |
|
49 | 52 | # Solid line on top |
50 | 53 | ax.plot(days, visitors, color="#306998", linewidth=3, zorder=3) |
51 | 54 |
|
| 55 | +# Annotate the viral spike |
| 56 | +ax.annotate( |
| 57 | + "Viral post", |
| 58 | + xy=(18, visitors[17]), |
| 59 | + xytext=(22, visitors[17] + 400), |
| 60 | + fontsize=16, |
| 61 | + color="#306998", |
| 62 | + fontweight="bold", |
| 63 | + arrowprops={"arrowstyle": "->", "color": "#306998", "lw": 2}, |
| 64 | + zorder=4, |
| 65 | +) |
| 66 | + |
52 | 67 | # Labels and styling |
53 | 68 | ax.set_xlabel("Day of Month", fontsize=20) |
54 | 69 | ax.set_ylabel("Daily Visitors (count)", fontsize=20) |
55 | | -ax.set_title("area-basic · matplotlib · pyplots.ai", fontsize=24) |
| 70 | +ax.set_title("Website Traffic · area-basic · matplotlib · pyplots.ai", fontsize=24) |
56 | 71 | ax.tick_params(axis="both", labelsize=16) |
57 | 72 | ax.grid(True, alpha=0.3, linestyle="--") |
58 | 73 |
|
|
0 commit comments