{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Tick locators\n\nTick locators define the position of the ticks.\n\nThis example illustrates the usage and effect of the most common locators.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.ticker as ticker\n\n\ndef setup(ax, title):\n \"\"\"Set up common parameters for the Axes in the example.\"\"\"\n # only show the bottom spine\n ax.yaxis.set_major_locator(ticker.NullLocator())\n ax.spines[['left', 'right', 'top']].set_visible(False)\n\n ax.xaxis.set_ticks_position('bottom')\n ax.tick_params(which='major', width=1.00, length=5)\n ax.tick_params(which='minor', width=0.75, length=2.5)\n ax.set_xlim(0, 5)\n ax.set_ylim(0, 1)\n ax.text(0.0, 0.2, title, transform=ax.transAxes,\n fontsize=14, fontname='Monospace', color='tab:blue')\n\n\nfig, axs = plt.subplots(8, 1, figsize=(8, 6))\n\n# Null Locator\nsetup(axs[0], title=\"NullLocator()\")\naxs[0].xaxis.set_major_locator(ticker.NullLocator())\naxs[0].xaxis.set_minor_locator(ticker.NullLocator())\n\n# Multiple Locator\nsetup(axs[1], title=\"MultipleLocator(0.5, offset=0.2)\")\naxs[1].xaxis.set_major_locator(ticker.MultipleLocator(0.5, offset=0.2))\naxs[1].xaxis.set_minor_locator(ticker.MultipleLocator(0.1))\n\n# Fixed Locator\nsetup(axs[2], title=\"FixedLocator([0, 1, 5])\")\naxs[2].xaxis.set_major_locator(ticker.FixedLocator([0, 1, 5]))\naxs[2].xaxis.set_minor_locator(ticker.FixedLocator(np.linspace(0.2, 0.8, 4)))\n\n# Linear Locator\nsetup(axs[3], title=\"LinearLocator(numticks=3)\")\naxs[3].xaxis.set_major_locator(ticker.LinearLocator(3))\naxs[3].xaxis.set_minor_locator(ticker.LinearLocator(31))\n\n# Index Locator\nsetup(axs[4], title=\"IndexLocator(base=0.5, offset=0.25)\")\naxs[4].plot([0]*5, color='white')\naxs[4].xaxis.set_major_locator(ticker.IndexLocator(base=0.5, offset=0.25))\n\n# Auto Locator\nsetup(axs[5], title=\"AutoLocator()\")\naxs[5].xaxis.set_major_locator(ticker.AutoLocator())\naxs[5].xaxis.set_minor_locator(ticker.AutoMinorLocator())\n\n# MaxN Locator\nsetup(axs[6], title=\"MaxNLocator(n=4)\")\naxs[6].xaxis.set_major_locator(ticker.MaxNLocator(4))\naxs[6].xaxis.set_minor_locator(ticker.MaxNLocator(40))\n\n# Log Locator\nsetup(axs[7], title=\"LogLocator(base=10, numticks=15)\")\naxs[7].set_xlim(10**3, 10**10)\naxs[7].set_xscale('log')\naxs[7].xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=15))\n\nplt.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The following functions, methods, classes and modules are used in this example:\n\n - `matplotlib.axis.Axis.set_major_locator`\n - `matplotlib.axis.Axis.set_minor_locator`\n - `matplotlib.ticker.NullLocator`\n - `matplotlib.ticker.MultipleLocator`\n - `matplotlib.ticker.FixedLocator`\n - `matplotlib.ticker.LinearLocator`\n - `matplotlib.ticker.IndexLocator`\n - `matplotlib.ticker.AutoLocator`\n - `matplotlib.ticker.MaxNLocator`\n - `matplotlib.ticker.LogLocator`\n\n" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 0 }