{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Automatically setting tick positions\n\nSetting the behavior of tick auto-placement.\n\nBy default, Matplotlib will choose the number of ticks and tick positions so\nthat there is a reasonable number of ticks on the axis and they are located\nat \"round\" numbers.\n\nAs a result, there may be no ticks on the edges of the plot.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nnp.random.seed(19680801)\n\nfig, ax = plt.subplots()\ndots = np.linspace(0.3, 1.2, 10)\nX, Y = np.meshgrid(dots, dots)\nx, y = X.ravel(), Y.ravel()\nax.scatter(x, y, c=x+y)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to keep ticks at round numbers, and also have ticks at the edges\nyou can switch :rc:`axes.autolimit_mode` to 'round_numbers'. This expands the\naxis limits to the next round number.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.rcParams['axes.autolimit_mode'] = 'round_numbers'\n\n# Note: The limits are calculated at draw-time. Therefore, when using\n# :rc:`axes.autolimit_mode` in a context manager, it is important that\n# the ``show()`` command is within the context.\n\nfig, ax = plt.subplots()\nax.scatter(x, y, c=x+y)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The round numbers autolimit_mode is still respected if you set an additional\nmargin around the data using `.Axes.set_xmargin` / `.Axes.set_ymargin`:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nax.scatter(x, y, c=x+y)\nax.set_xmargin(0.8)\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.2" } }, "nbformat": 4, "nbformat_minor": 0 }