-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatplotlib.py
More file actions
64 lines (52 loc) · 2.03 KB
/
matplotlib.py
File metadata and controls
64 lines (52 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
""" pyplots.ai
area-basic: Basic Area Chart
Library: matplotlib 3.10.8 | Python 3.14.2
Quality: 95/100 | Created: 2025-12-23
"""
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import PathPatch
from matplotlib.path import Path
# Data - daily website visitors over a month with weekend dips
np.random.seed(42)
days = np.arange(1, 31)
base_visitors = 5000 + np.linspace(0, 2500, 30) # Upward trend
weekend_effect = np.array([-1200 if d % 7 in (0, 6) else 0 for d in days]) # Weekend dips
noise = np.random.randn(30) * 400
visitors = base_visitors + weekend_effect + noise
visitors = np.clip(visitors, 2000, 10000)
# Create plot (4800x2700 px)
fig, ax = plt.subplots(figsize=(16, 9))
y_max = visitors.max() * 1.15
# Gradient fill using imshow clipped to the area shape
cmap = mcolors.LinearSegmentedColormap.from_list("area_grad", ["#d6e6f5", "#306998"])
gradient = np.linspace(0, 1, 256).reshape(-1, 1)
gradient = np.hstack([gradient, gradient])
# Build clip path manually from fill_between polygon
verts = [(days[0], 0)]
for d, v in zip(days, visitors, strict=True):
verts.append((d, v))
verts.append((days[-1], 0))
verts.append((days[0], 0))
codes = [Path.MOVETO] + [Path.LINETO] * (len(verts) - 2) + [Path.CLOSEPOLY]
clip_path = Path(verts, codes)
im = ax.imshow(
gradient, aspect="auto", cmap=cmap, alpha=0.6, extent=[days[0], days[-1], 0, y_max], origin="lower", zorder=1
)
patch = PathPatch(clip_path, transform=ax.transData, facecolor="none", edgecolor="none")
ax.add_patch(patch)
im.set_clip_path(patch)
# Solid line on top
ax.plot(days, visitors, color="#306998", linewidth=3, zorder=3)
# Labels and styling
ax.set_xlabel("Day of Month", fontsize=20)
ax.set_ylabel("Daily Visitors (count)", fontsize=20)
ax.set_title("area-basic · matplotlib · pyplots.ai", fontsize=24)
ax.tick_params(axis="both", labelsize=16)
ax.grid(True, alpha=0.3, linestyle="--")
# Set axis limits
ax.set_xlim(1, 30)
ax.set_ylim(0, y_max)
plt.tight_layout()
plt.savefig("plot.png", dpi=300, bbox_inches="tight")