{"spec_id":"line-multi","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nline-multi: Multi-Line Comparison Plot\nLibrary: plotly 6.9.0 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme tokens (see prompts/default-style-guide.md)\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\"\nGRID = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint palette (positions 1-4 for 4 series)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data - monthly sales (units) for 4 product lines over 12 months\nnp.random.seed(42)\nmonths = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n\n# Product sales with distinct trends: rising, seasonal decline, rising, flat\nelectronics = 150 + np.cumsum(np.random.randn(12) * 10) + np.linspace(0, 50, 12)\nclothing = 200 + np.cumsum(np.random.randn(12) * 8) + 20 * np.sin(np.linspace(0, 2 * np.pi, 12))\nhome_garden = 100 + np.cumsum(np.random.randn(12) * 6) + np.linspace(0, 30, 12)\nsports = 120 + np.cumsum(np.random.randn(12) * 12)\n\nseries = [\n    (\"Electronics\", electronics, \"circle\", \"solid\"),\n    (\"Clothing\", clothing, \"square\", \"solid\"),\n    (\"Home & Garden\", home_garden, \"diamond\", \"dash\"),\n    (\"Sports\", sports, \"triangle-up\", \"dot\"),\n]\n\n# Plot\nfig = go.Figure()\n\nfor i, (name, values, symbol, dash) in enumerate(series):\n    color = IMPRINT[i]\n    line_style = dict(color=color, width=3.5)\n    if dash != \"solid\":\n        line_style[\"dash\"] = dash\n    fig.add_trace(\n        go.Scatter(\n            x=months,\n            y=values,\n            name=name,\n            mode=\"lines+markers\",\n            line=line_style,\n            marker=dict(size=12, symbol=symbol, line=dict(width=1.5, color=PAGE_BG)),\n            hovertemplate=f\"{name}: %{{y:.0f}} units<extra></extra>\",\n        )\n    )\n    # Direct end-of-line value label - reinforces the closing value per\n    # series without competing for space with the (name-carrying) legend.\n    fig.add_annotation(\n        x=1.02,\n        xref=\"paper\",\n        y=values[-1],\n        yref=\"y\",\n        text=f\"{values[-1]:.0f}\",\n        showarrow=False,\n        xanchor=\"left\",\n        yanchor=\"middle\",\n        font=dict(size=14, color=color),\n    )\n\n# Callout highlighting the standout trend for data storytelling\ngrowth_pct = (electronics[-1] - electronics[0]) / electronics[0] * 100\nfig.add_annotation(\n    x=months[8],\n    y=electronics[8],\n    xref=\"x\",\n    yref=\"y\",\n    text=f\"Electronics up {growth_pct:.0f}% since Jan\",\n    showarrow=True,\n    arrowhead=2,\n    arrowwidth=1.5,\n    arrowcolor=INK_SOFT,\n    ax=-70,\n    ay=-45,\n    font=dict(size=13, color=INK),\n    bgcolor=ELEVATED_BG,\n    bordercolor=INK_SOFT,\n    borderwidth=1,\n    borderpad=6,\n)\n\n# Style\nfig.update_layout(\n    autosize=False,\n    title=dict(\n        text=\"line-multi · python · plotly · anyplot.ai\", font=dict(size=16, color=INK), x=0.5, xanchor=\"center\"\n    ),\n    xaxis=dict(\n        title=dict(text=\"Month\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        showgrid=True,\n        gridwidth=1,\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Sales (Units)\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        showgrid=True,\n        gridwidth=1,\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        # Headroom above the data max keeps the legend clear of the lines\n        # instead of overlapping the Jan/Feb Electronics & Clothing points.\n        range=[30, 300],\n    ),\n    legend=dict(\n        font=dict(size=10, color=INK_SOFT),\n        x=0.02,\n        y=0.98,\n        xanchor=\"left\",\n        yanchor=\"top\",\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    margin=dict(l=80, r=110, t=90, b=70),\n    hovermode=\"x unified\",\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}