{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Logit scale\n\nExamples of plots with logit axes.\n\nThis example visualises how ``set_yscale(\"logit\")`` works on probability plots\nby generating three distributions: normal, laplacian, and cauchy in one plot.\n\nThe advantage of logit scale is that it effectively spreads out values close to 0 and 1.\n\nIn a linear scale plot, probability values near 0 and 1 appear compressed,\nmaking it difficult to see differences in those regions.\n\nIn a logit scale plot, the transformation expands these regions,\nmaking the graph cleaner and easier to compare across different probability values.\n\nThis makes the logit scale especially useful when visalising probabilities in logistic\nregression, classification models, and cumulative distribution functions.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import math\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nxmax = 10\nx = np.linspace(-xmax, xmax, 10000)\ncdf_norm = [math.erf(w / np.sqrt(2)) / 2 + 1 / 2 for w in x]\ncdf_laplacian = np.where(x < 0, 1 / 2 * np.exp(x), 1 - 1 / 2 * np.exp(-x))\ncdf_cauchy = np.arctan(x) / np.pi + 1 / 2\n\nfig, axs = plt.subplots(nrows=3, ncols=2, figsize=(6.4, 8.5))\n\n# Common part, for the example, we will do the same plots on all graphs\nfor i in range(3):\n for j in range(2):\n axs[i, j].plot(x, cdf_norm, label=r\"$\\mathcal{N}$\")\n axs[i, j].plot(x, cdf_laplacian, label=r\"$\\mathcal{L}$\")\n axs[i, j].plot(x, cdf_cauchy, label=\"Cauchy\")\n axs[i, j].legend()\n axs[i, j].grid()\n\n# First line, logitscale, with standard notation\naxs[0, 0].set(title=\"logit scale\")\naxs[0, 0].set_yscale(\"logit\")\naxs[0, 0].set_ylim(1e-5, 1 - 1e-5)\n\naxs[0, 1].set(title=\"logit scale\")\naxs[0, 1].set_yscale(\"logit\")\naxs[0, 1].set_xlim(0, xmax)\naxs[0, 1].set_ylim(0.8, 1 - 5e-3)\n\n# Second line, logitscale, with survival notation (with `use_overline`), and\n# other format display 1/2\naxs[1, 0].set(title=\"logit scale\")\naxs[1, 0].set_yscale(\"logit\", one_half=\"1/2\", use_overline=True)\naxs[1, 0].set_ylim(1e-5, 1 - 1e-5)\n\naxs[1, 1].set(title=\"logit scale\")\naxs[1, 1].set_yscale(\"logit\", one_half=\"1/2\", use_overline=True)\naxs[1, 1].set_xlim(0, xmax)\naxs[1, 1].set_ylim(0.8, 1 - 5e-3)\n\n# Third line, linear scale\naxs[2, 0].set(title=\"linear scale\")\naxs[2, 0].set_ylim(0, 1)\n\naxs[2, 1].set(title=\"linear scale\")\naxs[2, 1].set_xlim(0, xmax)\naxs[2, 1].set_ylim(0.8, 1)\n\nfig.tight_layout()\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 0 }