{"spec_id":"bland-altman-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nbland-altman-basic: Bland-Altman Agreement Plot\nLibrary: matplotlib 3.11.1 | Python 3.13.14\nQuality: 93/100 | Updated: 2026-08-11\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport matplotlib.transforms as transforms\nimport numpy as np\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1 — first categorical series\nACCENT2 = \"#C475FD\"  # Okabe-Ito position 2 — for limits of agreement lines\n\n# Data: Simulated blood pressure readings from two sphygmomanometers\nnp.random.seed(42)\nn_samples = 80\n\n# Method 1: Reference sphygmomanometer\nmethod1 = np.random.normal(120, 15, n_samples)\n\n# Method 2: New sphygmomanometer — constant bias plus error that widens at\n# higher pressure readings (mild heteroscedasticity), the proportional-bias\n# pattern Bland-Altman plots are specifically designed to expose\nbias_true = 2.5\nerror_scale = 4.0 + 0.08 * (method1 - method1.min())\nmethod2 = method1 + bias_true + np.random.normal(0, 1, n_samples) * error_scale\n\n# Bland-Altman calculations\nmean_values = (method1 + method2) / 2\ndifferences = method1 - method2\n\nmean_diff = np.mean(differences)\nstd_diff = np.std(differences, ddof=1)\nupper_loa = mean_diff + 1.96 * std_diff\nlower_loa = mean_diff - 1.96 * std_diff\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Scatter points with transparency\nax.scatter(mean_values, differences, s=130, alpha=0.65, color=BRAND, edgecolors=PAGE_BG, linewidth=0.5)\n\n# Mean difference (bias) line\nax.axhline(y=mean_diff, color=BRAND, linestyle=\"-\", linewidth=2.5)\n\n# Limits of agreement (dashed lines)\nax.axhline(y=upper_loa, color=ACCENT2, linestyle=\"--\", linewidth=2.5)\nax.axhline(y=lower_loa, color=ACCENT2, linestyle=\"--\", linewidth=2.5)\n\n# Zero reference line (subtle)\nax.axhline(y=0, color=INK_SOFT, linestyle=\":\", linewidth=1.5, alpha=0.4)\n\n# Value annotations, anchored to the axes fraction in x (via a blended\n# transform) so they stay flush against the right edge regardless of the\n# data range — the bias line gets a plain label, the LOA lines get an\n# arrow-connected callout so the reader can trace label back to line\ntrans = transforms.blended_transform_factory(ax.transAxes, ax.transData)\nax.annotate(\n    f\"Bias: {mean_diff:.2f}\",\n    xy=(1.0, mean_diff),\n    xycoords=trans,\n    xytext=(12, 0),\n    textcoords=\"offset points\",\n    fontsize=8,\n    va=\"center\",\n    ha=\"left\",\n    color=INK,\n    fontweight=\"bold\",\n    annotation_clip=False,\n)\nfor loa_value, loa_label in ((upper_loa, f\"+1.96 SD: {upper_loa:.2f}\"), (lower_loa, f\"-1.96 SD: {lower_loa:.2f}\")):\n    ax.annotate(\n        loa_label,\n        xy=(1.0, loa_value),\n        xycoords=trans,\n        xytext=(24, 0),\n        textcoords=\"offset points\",\n        fontsize=8,\n        va=\"center\",\n        ha=\"left\",\n        color=INK_SOFT,\n        arrowprops={\"arrowstyle\": \"->\", \"color\": INK_SOFT, \"linewidth\": 0.8, \"shrinkA\": 0, \"shrinkB\": 3},\n        annotation_clip=False,\n    )\n\n# Labels and title\nax.set_xlabel(\"Mean of Two Methods (mmHg)\", fontsize=10, color=INK)\nax.set_ylabel(\"Difference (Method 1 - Method 2) (mmHg)\", fontsize=10, color=INK)\nax.set_title(\"bland-altman-basic · python · matplotlib · anyplot.ai\", fontsize=12, color=INK, fontweight=\"medium\")\n\n# Tick parameters\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\n\n# Spines\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\n\n# Grid\nax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK)\n\n# Adjust layout — right margin reserved for the line-anchored annotations\nplt.tight_layout()\nplt.subplots_adjust(right=0.82)\n\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}