{"spec_id":"audiogram-clinical","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\naudiogram-clinical: Clinical Audiogram\nLibrary: matplotlib 3.11.0 | Python 3.13.13\nQuality: 91/100 | Created: 2026-06-14\n\"\"\"\n\nimport sys\n\n\n# Prevent this file from shadowing the installed matplotlib package when run\n# from its own directory (sys.path[0] would otherwise resolve to this file).\nsys.path.pop(0)\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport matplotlib.ticker as ticker\nfrom matplotlib.transforms import blended_transform_factory\n\n\n# Theme tokens (see prompts/default-style-guide.md \"Theme-adaptive Chrome\")\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# Ear colors — semantic exception from Imprint palette (red=right, blue=left per audiological convention)\nRIGHT_EAR = \"#AE3030\"  # Imprint matte red — right-ear convention\nLEFT_EAR = \"#4467A3\"  # Imprint blue — left-ear convention\n\n# Data — noise-induced high-frequency sensorineural hearing loss (occupational pattern)\nfrequencies = [125, 250, 500, 1000, 2000, 4000, 8000]\nthreshold_right = [10, 10, 15, 15, 25, 55, 65]  # right ear (dB HL)\nthreshold_left = [15, 15, 20, 20, 30, 65, 75]  # left ear (dB HL)\n\n# Severity bands: (y_start, y_end, fill_color, alpha, label)\nseverity_bands = [\n    (-10, 25, \"#009E73\", 0.07, \"Normal\"),\n    (25, 40, \"#DDCC77\", 0.10, \"Mild\"),\n    (40, 55, \"#BD8233\", 0.10, \"Moderate\"),\n    (55, 70, \"#AE3030\", 0.09, \"Mod. Severe\"),\n    (70, 90, \"#AE3030\", 0.17, \"Severe\"),\n    (90, 120, \"#AE3030\", 0.27, \"Profound\"),\n]\n\n# Plot — square canvas for standardized audiogram layout\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Severity band shading\nfor y_start, y_end, color, alpha, _ in severity_bands:\n    ax.axhspan(y_start, y_end, alpha=alpha, color=color, zorder=0, lw=0)\n\n# Band labels on right margin outside axes\nband_trans = blended_transform_factory(ax.transAxes, ax.transData)\nfor y_start, y_end, _, _, label in severity_bands:\n    mid = (y_start + y_end) / 2\n    ax.text(\n        1.025, mid, label, transform=band_trans, fontsize=7.5, color=INK_MUTED, va=\"center\", ha=\"left\", clip_on=False\n    )\n\n# Right ear — open circle (O) markers with solid connecting line\nax.plot(\n    frequencies,\n    threshold_right,\n    marker=\"o\",\n    linestyle=\"-\",\n    color=RIGHT_EAR,\n    linewidth=2.2,\n    markersize=10,\n    markerfacecolor=\"none\",\n    markeredgewidth=2.2,\n    markeredgecolor=RIGHT_EAR,\n    label=\"Right Ear (O)\",\n    zorder=3,\n)\n\n# Left ear — cross (X) markers with solid connecting line\nax.plot(\n    frequencies,\n    threshold_left,\n    marker=\"x\",\n    linestyle=\"-\",\n    color=LEFT_EAR,\n    linewidth=2.2,\n    markersize=11,\n    markeredgewidth=2.5,\n    label=\"Left Ear (X)\",\n    zorder=3,\n)\n\n# X-axis — logarithmic, standard audiometric frequencies\nax.set_xscale(\"log\")\nax.set_xlim(88, 9500)\nax.set_xticks(frequencies)\nax.set_xticklabels([\"125\", \"250\", \"500\", \"1k\", \"2k\", \"4k\", \"8k\"])\nax.xaxis.set_minor_locator(ticker.NullLocator())\n\n# Y-axis — inverted (0 dB HL at top), range -10 to 120, gridlines every 10 dB\nax.set_ylim(120, -10)\nax.set_yticks(range(-10, 130, 10))\n\n# Grid — every 10 dB and at each standard frequency\nax.grid(True, which=\"major\", alpha=0.18, linewidth=0.8, color=INK, zorder=1)\n\n# Title with scaled fontsize\ntitle = \"audiogram-clinical · python · matplotlib · anyplot.ai\"\nn = len(title)\ntitle_fs = max(8, round(12 * 67 / n)) if n > 67 else 12\n\n# Chrome styling\nax.set_title(title, fontsize=title_fs, fontweight=\"medium\", color=INK, pad=10)\nax.set_xlabel(\"Frequency (Hz)\", fontsize=10, color=INK, labelpad=6)\nax.set_ylabel(\"Hearing Level (dB HL)\", fontsize=10, color=INK, labelpad=6)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, length=0)\n\nfor spine in ax.spines.values():\n    spine.set_color(INK_SOFT)\n    spine.set_linewidth(0.8)\n\n# Legend — placed in lower-left (high dB HL region, no data)\nleg = ax.legend(fontsize=8, loc=\"lower left\")\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nplt.setp(leg.get_texts(), color=INK_SOFT)\n\n# Layout — explicit margins so severity labels on the right are not clipped\nfig.subplots_adjust(left=0.13, right=0.82, top=0.93, bottom=0.12)\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}