{"spec_id":"line-yield-curve","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nline-yield-curve: Yield Curve (Interest Rate Term Structure)\nLibrary: bokeh 3.9.1 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-06-10\n\"\"\"\n\nimport base64\nimport sys\n\n\n# Remove the script's own directory from sys.path so 'bokeh' resolves to the\n# installed package, not this file (the filename 'bokeh.py' would shadow it).\nsys.path.pop(0)\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, Label, Legend, LegendItem, NumeralTickFormatter\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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# Imprint palette — canonical order, theme-independent\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\ncolor_normal = IMPRINT_PALETTE[0]  # brand green: Jan 2024 — Normal\ncolor_flat = IMPRINT_PALETTE[1]  # lavender: Jun 2024 — Flat\ncolor_inverted = IMPRINT_PALETTE[2]  # blue: Jul 2023 — Inverted\n\n# Data - U.S. Treasury yield curves on three representative dates\nmaturity_labels = [\"1M\", \"3M\", \"6M\", \"1Y\", \"2Y\", \"3Y\", \"5Y\", \"7Y\", \"10Y\", \"20Y\", \"30Y\"]\nmaturity_years = np.array([1 / 12, 0.25, 0.5, 1, 2, 3, 5, 7, 10, 20, 30])\n\nyields_normal = np.array([5.53, 5.46, 5.36, 4.95, 4.42, 4.15, 3.98, 4.03, 4.10, 4.38, 4.27])\nyields_flat = np.array([5.47, 5.49, 5.40, 5.12, 4.75, 4.55, 4.32, 4.30, 4.28, 4.52, 4.40])\nyields_inverted = np.array([5.47, 5.52, 5.56, 5.40, 4.87, 4.56, 4.18, 4.09, 3.96, 4.22, 4.03])\n\nsource_normal = ColumnDataSource(data={\"maturity\": maturity_years, \"yield_pct\": yields_normal})\nsource_flat = ColumnDataSource(data={\"maturity\": maturity_years, \"yield_pct\": yields_flat})\nsource_inverted = ColumnDataSource(data={\"maturity\": maturity_years, \"yield_pct\": yields_inverted})\n\n# Inversion zone: region where short-term yields exceed the 30Y long-end yield\ninv_30y = yields_inverted[-1]\ninv_mask = yields_inverted > inv_30y\ninv_x = maturity_years[inv_mask]\ninv_upper = yields_inverted[inv_mask]\nsource_inv_shade = ColumnDataSource(data={\"x\": inv_x, \"y1\": np.full_like(inv_x, inv_30y), \"y2\": inv_upper})\n\n# Title — 46 chars < 67 baseline, default 50pt applies\ntitle = \"line-yield-curve · python · bokeh · anyplot.ai\"\n\n# Figure — canonical 3200×1800, toolbar disabled in constructor to prevent PNG height drift\np = figure(\n    width=3200,\n    height=1800,\n    title=title,\n    x_axis_label=\"Maturity\",\n    y_axis_label=\"Yield (%)\",\n    x_axis_type=\"log\",\n    x_range=(0.06, 42),\n    y_range=(3.75, 5.8),\n    toolbar_location=None,\n    min_border_bottom=160,\n    min_border_left=180,\n    min_border_top=110,\n    min_border_right=50,\n)\n\n# Inversion zone shading — alpha increased for visibility\np.varea(x=\"x\", y1=\"y1\", y2=\"y2\", source=source_inv_shade, fill_color=color_inverted, fill_alpha=0.18)\n\n# Normal curve\nline_normal = p.line(x=\"maturity\", y=\"yield_pct\", source=source_normal, line_width=4.5, line_color=color_normal)\nscatter_normal = p.scatter(\n    x=\"maturity\",\n    y=\"yield_pct\",\n    source=source_normal,\n    size=16,\n    fill_color=color_normal,\n    line_color=PAGE_BG,\n    line_width=2.5,\n)\n\n# Flat curve\nline_flat = p.line(x=\"maturity\", y=\"yield_pct\", source=source_flat, line_width=4.5, line_color=color_flat)\nscatter_flat = p.scatter(\n    x=\"maturity\", y=\"yield_pct\", source=source_flat, size=16, fill_color=color_flat, line_color=PAGE_BG, line_width=2.5\n)\n\n# Inverted curve — dashed to visually emphasize the anomalous shape\nline_inverted = p.line(\n    x=\"maturity\", y=\"yield_pct\", source=source_inverted, line_width=4.5, line_color=color_inverted, line_dash=[12, 6]\n)\nscatter_inverted = p.scatter(\n    x=\"maturity\",\n    y=\"yield_pct\",\n    source=source_inverted,\n    size=16,\n    fill_color=color_inverted,\n    line_color=PAGE_BG,\n    line_width=2.5,\n)\n\n# Subtitle — increased font size for canvas legibility\nsubtitle = Label(\n    x=0.07,\n    y=5.65,\n    text=\"U.S. Treasury Yield Curves — normal, flat, and inverted term structures\",\n    text_font_size=\"28pt\",\n    text_color=INK_MUTED,\n)\np.add_layout(subtitle)\n\n# Inversion annotation — increased font size for canvas legibility\ninversion_label = Label(\n    x=0.55,\n    y=3.87,\n    text=\"Inversion zone: short-term yields exceed long-term\",\n    text_font_size=\"28pt\",\n    text_color=color_inverted,\n    text_font_style=\"italic\",\n    text_alpha=0.9,\n)\np.add_layout(inversion_label)\n\n# Legend — theme-adaptive background and labels\nlegend = Legend(\n    items=[\n        LegendItem(label=\"Jan 2024 — Normal\", renderers=[line_normal, scatter_normal]),\n        LegendItem(label=\"Jun 2024 — Flat\", renderers=[line_flat, scatter_flat]),\n        LegendItem(label=\"Jul 2023 — Inverted\", renderers=[line_inverted, scatter_inverted]),\n    ],\n    location=\"bottom_right\",\n)\nlegend.label_text_font_size = \"34pt\"\nlegend.label_text_color = INK_SOFT\nlegend.glyph_width = 55\nlegend.glyph_height = 32\nlegend.spacing = 16\nlegend.padding = 28\nlegend.margin = 30\nlegend.background_fill_color = ELEVATED_BG\nlegend.border_line_color = INK_SOFT\np.add_layout(legend)\n\n# Custom log-axis tick labels using maturity string labels\np.xaxis.ticker = list(maturity_years)\np.xaxis.major_label_overrides = {maturity_years[i]: maturity_labels[i] for i in range(len(maturity_labels))}\n\n# Theme-adaptive chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = None\n\np.title.text_font_size = \"50pt\"\np.title.text_color = INK\np.title.text_font_style = \"bold\"\n\np.xaxis.axis_label_text_font_size = \"42pt\"\np.yaxis.axis_label_text_font_size = \"42pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"34pt\"\np.yaxis.major_label_text_font_size = \"34pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = None\np.yaxis.major_tick_line_color = None\np.xaxis.minor_tick_line_color = None\np.yaxis.minor_tick_line_color = None\n\n# Grid — subtle y-axis only, x-grid hidden\np.xgrid.grid_line_alpha = 0\np.ygrid.grid_line_alpha = 0.15\np.ygrid.grid_line_color = INK\np.ygrid.grid_line_width = 1\n\np.yaxis.formatter = NumeralTickFormatter(format=\"0.0\")\n\n# Save interactive HTML (catalog artifact)\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot via headless Chrome — use CDP captureBeyondViewport to get exact W×H\nW, H = 3200, 1800\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n    \"--force-device-scale-factor=1\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\nscreenshot = driver.execute_cdp_cmd(\n    \"Page.captureScreenshot\",\n    {\"format\": \"png\", \"captureBeyondViewport\": True, \"clip\": {\"x\": 0, \"y\": 0, \"width\": W, \"height\": H, \"scale\": 1}},\n)\nPath(f\"plot-{THEME}.png\").write_bytes(base64.b64decode(screenshot[\"data\"]))\ndriver.quit()\n"}