-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseaborn.py
More file actions
64 lines (51 loc) · 2.03 KB
/
Copy pathseaborn.py
File metadata and controls
64 lines (51 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: seaborn 0.13.2 | Python 3.14.2
Quality: 98/100 | Created: 2025-12-23
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
# Data - daily website visitors over a month
np.random.seed(42)
dates = pd.date_range(start="2024-01-01", periods=30, freq="D")
# Simulate realistic web traffic with weekly pattern, trend, and a traffic spike
base_visitors = 5000
trend = np.linspace(0, 1500, 30)
weekly_pattern = np.array([1.0, 1.1, 1.15, 1.2, 1.1, 0.7, 0.65] * 5)[:30]
noise = np.random.randn(30) * 300
visitors = (base_visitors + trend) * weekly_pattern + noise
visitors[17] *= 1.45 # Traffic spike from a viral post on day 18
visitors = np.maximum(visitors, 1000)
df = pd.DataFrame({"date": dates, "visitors": visitors})
# Plot - seaborn styling and theme
sns.set_style("whitegrid", {"grid.linestyle": "--", "grid.alpha": 0.3})
sns.set_context("talk", font_scale=1.1)
fig, ax = plt.subplots(figsize=(16, 9))
# Area chart using seaborn lineplot with fill
sns.lineplot(data=df, x="date", y="visitors", ax=ax, color="#306998", linewidth=3, label="Daily visitors")
ax.fill_between(df["date"], df["visitors"], alpha=0.4, color="#306998")
# Annotate the traffic spike
spike_idx = 17
ax.annotate(
"Viral post",
xy=(df["date"].iloc[spike_idx], df["visitors"].iloc[spike_idx]),
xytext=(df["date"].iloc[spike_idx - 6], df["visitors"].iloc[spike_idx] * 1.0),
fontsize=16,
color="#1a3a5c",
arrowprops={"arrowstyle": "->", "color": "#1a3a5c", "lw": 2},
ha="center",
)
# Style
ax.set_xlabel("Date", fontsize=20)
ax.set_ylabel("Visitors (count)", fontsize=20)
ax.set_title("area-basic · seaborn · pyplots.ai", fontsize=24)
ax.tick_params(axis="both", labelsize=16)
ax.legend(fontsize=16, loc="upper left", framealpha=0.9)
# Set y-axis to start at 0, cap top to reduce whitespace
ax.set_ylim(bottom=0, top=df["visitors"].max() * 1.12)
# Format x-axis dates
fig.autofmt_xdate(rotation=45)
plt.tight_layout()
plt.savefig("plot.png", dpi=300, bbox_inches="tight")