{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Major and minor ticks\n\nDemonstrate how to use major and minor tickers.\n\nThe two relevant classes are `.Locator`\\s and `.Formatter`\\s. Locators\ndetermine where the ticks are, and formatters control the formatting of tick\nlabels.\n\nMinor ticks are off by default (using `.NullLocator` and `.NullFormatter`).\nMinor ticks can be turned on without labels by setting the minor locator.\nMinor tick labels can be turned on by setting the minor formatter.\n\n`.MultipleLocator` places ticks on multiples of some base.\n`.StrMethodFormatter` uses a format string (e.g., ``'{x:d}'`` or ``'{x:1.2f}'``\nor ``'{x:1.1f} cm'``) to format the tick labels (the variable in the format\nstring must be ``'x'``). For a `.StrMethodFormatter`, the string can be passed\ndirectly to `.Axis.set_major_formatter` or\n`.Axis.set_minor_formatter`. An appropriate `.StrMethodFormatter` will\nbe created and used automatically.\n\n`.pyplot.grid` changes the grid settings of the major ticks of the x- and\ny-axis together. If you want to control the grid of the minor ticks for a\ngiven axis, use for example ::\n\n ax.xaxis.grid(True, which='minor')\n\nNote that a given locator or formatter instance can only be used on a single\naxis (because the locator stores references to the axis data and view limits).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.ticker import AutoMinorLocator, MultipleLocator\n\nt = np.arange(0.0, 100.0, 0.1)\ns = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)\n\nfig, ax = plt.subplots()\nax.plot(t, s)\n\n# Make a plot with major ticks that are multiples of 20 and minor ticks that\n# are multiples of 5. Label major ticks with '.0f' formatting but don't label\n# minor ticks. The string is used directly, the `StrMethodFormatter` is\n# created automatically.\nax.xaxis.set_major_locator(MultipleLocator(20))\nax.xaxis.set_major_formatter('{x:.0f}')\n\n# For the minor ticks, use no labels; default NullFormatter.\nax.xaxis.set_minor_locator(MultipleLocator(5))\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Automatic tick selection for major and minor ticks.\n\nUse interactive pan and zoom to see how the tick intervals change. There will\nbe either 4 or 5 minor tick intervals per major interval, depending on the\nmajor interval.\n\nOne can supply an argument to `.AutoMinorLocator` to specify a fixed number\nof minor intervals per major interval, e.g. ``AutoMinorLocator(2)`` would\nlead to a single minor tick between major ticks.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t = np.arange(0.0, 100.0, 0.01)\ns = np.sin(2 * np.pi * t) * np.exp(-t * 0.01)\n\nfig, ax = plt.subplots()\nax.plot(t, s)\n\nax.xaxis.set_minor_locator(AutoMinorLocator())\n\nax.tick_params(which='both', width=2)\nax.tick_params(which='major', length=7)\nax.tick_params(which='minor', length=4, color='r')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.pyplot.subplots`\n - `matplotlib.axis.Axis.set_major_formatter`\n - `matplotlib.axis.Axis.set_major_locator`\n - `matplotlib.axis.Axis.set_minor_locator`\n - `matplotlib.ticker.AutoMinorLocator`\n - `matplotlib.ticker.MultipleLocator`\n - `matplotlib.ticker.StrMethodFormatter`\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 }