{"spec_id":"depth-order-book","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\ndepth-order-book: Order Book Depth Chart\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 88/100 | Created: 2026-06-15\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove this script's directory from sys.path so 'import seaborn' finds the installed\n# library rather than this file (Python adds the script dir to sys.path[0] automatically)\n_script_dir = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p or \".\") != _script_dir]\n\nimport matplotlib.pyplot as plt\nimport matplotlib.ticker as mticker\nimport numpy as np\nimport seaborn as sns\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Semantic colors — bid=green (buy/bullish), ask=red (sell/bearish)\nBID_COLOR = \"#009E73\"  # Imprint palette position 1, brand green — buy side\nASK_COLOR = \"#AE3030\"  # Imprint palette position 5, matte red — sell side\n\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.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data — BTC/USD order book snapshot (synthetic)\nnp.random.seed(42)\nmid_price = 60_000.0\nspread = 10.0  # $10 bid-ask spread\nbest_bid = mid_price - spread / 2  # 59995.0\nbest_ask = mid_price + spread / 2  # 60005.0\nn_levels = 40\ntick = 5.0  # $5 per price level\n\n# Bid levels: descending from best_bid (index 0 = best bid, index 39 = worst bid)\nbid_prices = best_bid - tick * np.arange(n_levels)\nbid_qty = np.abs(np.random.normal(2.0, 1.0, n_levels))\nbid_qty[14:17] += 16.0  # support wall at ~59925\nbid_cum = np.cumsum(bid_qty)\n\n# Ask levels: ascending from best_ask (index 0 = best ask, index 39 = worst ask)\nask_prices = best_ask + tick * np.arange(n_levels)\nask_qty = np.abs(np.random.normal(2.0, 1.0, n_levels))\nask_qty[17:20] += 20.0  # resistance wall at ~60090\nask_cum = np.cumsum(ask_qty)\n\n# Bid sorted ascending for left-to-right staircase rendering\nbid_x = bid_prices[::-1]  # [59800, ..., 59995]\nbid_y = bid_cum[::-1]  # [total_cum, ..., min_cum]\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Dark mode: raise fill alpha so areas remain visible against near-black background\nFILL_ALPHA = 0.22 if THEME == \"light\" else 0.35\n\n# Bid area: semi-transparent fill + solid step outline via seaborn lineplot\nax.fill_between(bid_x, bid_y, step=\"post\", alpha=FILL_ALPHA, color=BID_COLOR)\nsns.lineplot(x=bid_x, y=bid_y, drawstyle=\"steps-post\", ax=ax, color=BID_COLOR, linewidth=1.8, label=\"Bids (Buy)\")\n\n# Ask area: semi-transparent fill + solid step outline via seaborn lineplot\nax.fill_between(ask_prices, ask_cum, step=\"post\", alpha=FILL_ALPHA, color=ASK_COLOR)\nsns.lineplot(\n    x=ask_prices, y=ask_cum, drawstyle=\"steps-post\", ax=ax, color=ASK_COLOR, linewidth=1.8, label=\"Asks (Sell)\"\n)\n\n# Mid price dashed vertical line\nax.axvline(mid_price, color=INK_SOFT, linestyle=\"--\", linewidth=1.0, alpha=0.7)\n\n# Annotate mid price and spread\ny_top = max(bid_y.max(), ask_cum.max())\nax.annotate(\n    f\"Mid: ${mid_price:,.0f}\\nSpread: ${spread:.0f}\",\n    xy=(mid_price, y_top * 0.80),\n    ha=\"center\",\n    va=\"top\",\n    fontsize=8,\n    color=INK_MUTED,\n    bbox={\"boxstyle\": \"round,pad=0.4\", \"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"linewidth\": 0.8, \"alpha\": 0.9},\n)\n\n# Style\ntitle = \"depth-order-book · python · seaborn · anyplot.ai\"\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK, pad=10)\nax.set_xlabel(\"Price (USD)\", fontsize=10, color=INK, labelpad=6)\nax.set_ylabel(\"Cumulative Volume (BTC)\", fontsize=10, color=INK, labelpad=6)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\n\nax.xaxis.set_major_formatter(mticker.FuncFormatter(lambda x, _: f\"${x:,.0f}\"))\n\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\nax.set_ylim(bottom=0)\n\nlegend = ax.legend(fontsize=8, framealpha=0.9, facecolor=ELEVATED_BG, edgecolor=INK_SOFT)\nfor text in legend.get_texts():\n    text.set_color(INK_SOFT)\n\nfig.subplots_adjust(left=0.10, right=0.97, top=0.92, bottom=0.13)\n\n# Save to the script's own directory (no bbox_inches='tight' — see seaborn library guide)\nplt.savefig(os.path.join(_script_dir, f\"plot-{THEME}.png\"), dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}