{"spec_id":"violin-box","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nviolin-box: Violin Plot with Embedded Box Plot\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-12\n\"\"\"\n\nimport importlib\nimport os\nimport sys\n\n\n# Avoid importing from local directory\nfor path in list(sys.path):\n    if \"violin-box\" in path or \"implementations\" in path:\n        sys.path.remove(path)\n\ngo = importlib.import_module(\"plotly.graph_objects\")\nnp = importlib.import_module(\"numpy\")\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\n\n# Okabe-Ito palette — first series is always #009E73\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data — Test scores across different teaching methods\nnp.random.seed(42)\n\ngroups = [\"Traditional\", \"Interactive\", \"Online\", \"Hybrid\"]\ndata = {\n    \"Traditional\": np.random.normal(70, 12, 80),\n    \"Interactive\": np.random.normal(78, 10, 85),\n    \"Online\": np.concatenate([np.random.normal(55, 8, 40), np.random.normal(80, 7, 45)]),\n    \"Hybrid\": np.random.normal(75, 15, 90),\n}\n\n# Clip to realistic test score range (0-100)\nfor group in groups:\n    data[group] = np.clip(data[group], 0, 100)\n\n# Plot\nfig = go.Figure()\n\nfor i, group in enumerate(groups):\n    fig.add_trace(\n        go.Violin(\n            y=data[group],\n            name=group,\n            box_visible=True,\n            meanline_visible=True,\n            fillcolor=IMPRINT[i],\n            opacity=0.7,\n            line={\"color\": INK_SOFT, \"width\": 2},\n            points=\"outliers\",\n            pointpos=0,\n            marker={\"size\": 8, \"color\": INK_SOFT, \"opacity\": 0.6},\n            box={\"fillcolor\": PAGE_BG, \"line\": {\"color\": INK_SOFT, \"width\": 2}, \"width\": 0.15},\n            meanline={\"color\": IMPRINT[i], \"width\": 3},\n        )\n    )\n\n# Style\nfig.update_layout(\n    title={\n        \"text\": \"violin-box · plotly · anyplot.ai\",\n        \"font\": {\"size\": 28, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"title\": {\"text\": \"Teaching Method\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"showgrid\": False,\n        \"linecolor\": INK_SOFT,\n    },\n    yaxis={\n        \"title\": {\"text\": \"Test Score (points)\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"gridcolor\": GRID,\n        \"gridwidth\": 1,\n        \"range\": [0, 105],\n        \"linecolor\": INK_SOFT,\n    },\n    template=\"plotly_white\",\n    showlegend=False,\n    plot_bgcolor=PAGE_BG,\n    paper_bgcolor=PAGE_BG,\n    margin={\"l\": 100, \"r\": 50, \"t\": 100, \"b\": 100},\n    violingap=0.3,\n    violinmode=\"group\",\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}