{"spec_id":"line-annotated-events","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nline-annotated-events: Annotated Line Plot with Event Markers\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib.lines import Line2D\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Okabe-Ito palette\nBRAND = \"#009E73\"  # Position 1 - bluish green for main series\nEVENT_COLOR = \"#954477\"  # Position 7 - yellow for annotations\n\n# Configure seaborn with theme tokens\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data - Simulating monthly product sales with marketing events\nnp.random.seed(42)\n\n# Create 365 days of sales data\ndates = pd.date_range(\"2024-01-01\", periods=365, freq=\"D\")\n# Base trend with seasonality and noise\ntrend = np.linspace(100, 180, 365)\nseasonality = 15 * np.sin(np.linspace(0, 4 * np.pi, 365))\nnoise = np.random.normal(0, 8, 365)\nsales = trend + seasonality + noise\n\ndf = pd.DataFrame({\"date\": dates, \"sales\": sales})\n\n# Events - Key marketing milestones\nevents = pd.DataFrame(\n    {\n        \"event_date\": pd.to_datetime([\"2024-02-14\", \"2024-05-01\", \"2024-07-15\", \"2024-09-20\", \"2024-11-25\"]),\n        \"event_label\": [\"Valentine's Campaign\", \"Spring Sale\", \"Summer Launch\", \"Fall Promotion\", \"Black Friday\"],\n    }\n)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\n\n# Main line plot using seaborn\nsns.lineplot(data=df, x=\"date\", y=\"sales\", ax=ax, linewidth=2.5, color=BRAND)\n\n# Add event markers with alternating heights for readability\ny_positions = [0.85, 0.75, 0.85, 0.75, 0.85]\n\nfor i, (_, event) in enumerate(events.iterrows()):\n    # Vertical line at event date\n    ax.axvline(x=event[\"event_date\"], color=EVENT_COLOR, linestyle=\"--\", linewidth=2, alpha=0.8)\n\n    # Event label with background\n    y_pos = ax.get_ylim()[0] + (ax.get_ylim()[1] - ax.get_ylim()[0]) * y_positions[i]\n    ax.annotate(\n        event[\"event_label\"],\n        xy=(event[\"event_date\"], y_pos),\n        fontsize=14,\n        fontweight=\"bold\",\n        color=INK,\n        ha=\"center\",\n        va=\"bottom\",\n        bbox={\"boxstyle\": \"round,pad=0.3\", \"facecolor\": EVENT_COLOR, \"edgecolor\": \"none\", \"alpha\": 0.9},\n        rotation=0,\n    )\n\n    # Small marker on the line at event date\n    event_sales = df.loc[df[\"date\"] == event[\"event_date\"], \"sales\"]\n    if not event_sales.empty:\n        ax.scatter(\n            event[\"event_date\"], event_sales.values[0], color=EVENT_COLOR, s=150, zorder=5, edgecolor=INK, linewidth=2\n        )\n\n# Styling\nax.set_xlabel(\"Date\", fontsize=20, color=INK)\nax.set_ylabel(\"Daily Sales (Units)\", fontsize=20, color=INK)\nax.set_title(\"line-annotated-events · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Remove top and right spines\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor spine in [\"left\", \"bottom\"]:\n    ax.spines[spine].set_color(INK_SOFT)\n\n# Subtle y-axis grid\nax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK)\n\n# Add legend explaining event markers\nlegend_elements = [\n    Line2D([0], [0], color=BRAND, lw=2.5, label=\"Daily Sales\"),\n    Line2D([0], [0], color=EVENT_COLOR, lw=2, linestyle=\"--\", label=\"Event Marker\"),\n]\nax.legend(handles=legend_elements, loc=\"upper left\", fontsize=16, frameon=True, fancybox=True)\n\n# Format x-axis dates\nfig.autofmt_xdate(rotation=30)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}