|
| 1 | +""" pyplots.ai |
| 2 | +line-annotated-events: Annotated Line Plot with Event Markers |
| 3 | +Library: seaborn 0.13.2 | Python 3.13.11 |
| 4 | +Quality: 92/100 | Created: 2025-12-31 |
| 5 | +""" |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +import seaborn as sns |
| 11 | + |
| 12 | + |
| 13 | +# Data - Simulating monthly product sales with marketing events |
| 14 | +np.random.seed(42) |
| 15 | + |
| 16 | +# Create 12 months of sales data |
| 17 | +dates = pd.date_range("2024-01-01", periods=365, freq="D") |
| 18 | +# Base trend with seasonality and noise |
| 19 | +trend = np.linspace(100, 180, 365) |
| 20 | +seasonality = 15 * np.sin(np.linspace(0, 4 * np.pi, 365)) |
| 21 | +noise = np.random.normal(0, 8, 365) |
| 22 | +sales = trend + seasonality + noise |
| 23 | + |
| 24 | +df = pd.DataFrame({"date": dates, "sales": sales}) |
| 25 | + |
| 26 | +# Events - Key marketing milestones |
| 27 | +events = pd.DataFrame( |
| 28 | + { |
| 29 | + "event_date": pd.to_datetime(["2024-02-14", "2024-05-01", "2024-07-15", "2024-09-20", "2024-11-25"]), |
| 30 | + "event_label": ["Valentine's Campaign", "Spring Sale", "Summer Launch", "Fall Promotion", "Black Friday"], |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +# Plot |
| 35 | +fig, ax = plt.subplots(figsize=(16, 9)) |
| 36 | + |
| 37 | +# Main line plot using seaborn |
| 38 | +sns.lineplot(data=df, x="date", y="sales", ax=ax, linewidth=2.5, color="#306998") |
| 39 | + |
| 40 | +# Add event markers with alternating heights for readability |
| 41 | +y_positions = [0.85, 0.75, 0.85, 0.75, 0.85] # Alternating label positions |
| 42 | +colors_events = ["#FFD43B"] * len(events) # Use Python Yellow for all events |
| 43 | + |
| 44 | +for i, (_, event) in enumerate(events.iterrows()): |
| 45 | + # Vertical line at event date |
| 46 | + ax.axvline(x=event["event_date"], color=colors_events[i], linestyle="--", linewidth=2, alpha=0.8) |
| 47 | + |
| 48 | + # Event label with background |
| 49 | + y_pos = ax.get_ylim()[0] + (ax.get_ylim()[1] - ax.get_ylim()[0]) * y_positions[i] |
| 50 | + ax.annotate( |
| 51 | + event["event_label"], |
| 52 | + xy=(event["event_date"], y_pos), |
| 53 | + fontsize=14, |
| 54 | + fontweight="bold", |
| 55 | + color="#333333", |
| 56 | + ha="center", |
| 57 | + va="bottom", |
| 58 | + bbox={"boxstyle": "round,pad=0.3", "facecolor": "#FFD43B", "edgecolor": "none", "alpha": 0.9}, |
| 59 | + rotation=0, |
| 60 | + ) |
| 61 | + |
| 62 | + # Small marker on the line at event date |
| 63 | + event_sales = df.loc[df["date"] == event["event_date"], "sales"] |
| 64 | + if not event_sales.empty: |
| 65 | + ax.scatter( |
| 66 | + event["event_date"], |
| 67 | + event_sales.values[0], |
| 68 | + color="#FFD43B", |
| 69 | + s=150, |
| 70 | + zorder=5, |
| 71 | + edgecolor="#333333", |
| 72 | + linewidth=2, |
| 73 | + ) |
| 74 | + |
| 75 | +# Styling |
| 76 | +ax.set_xlabel("Date", fontsize=20) |
| 77 | +ax.set_ylabel("Daily Sales (Units)", fontsize=20) |
| 78 | +ax.set_title("line-annotated-events · seaborn · pyplots.ai", fontsize=24) |
| 79 | +ax.tick_params(axis="both", labelsize=16) |
| 80 | +ax.grid(True, alpha=0.3, linestyle="--") |
| 81 | + |
| 82 | +# Format x-axis dates |
| 83 | +fig.autofmt_xdate(rotation=30) |
| 84 | + |
| 85 | +plt.tight_layout() |
| 86 | +plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
0 commit comments