
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/ticks/auto_ticks.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_ticks_auto_ticks.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_ticks_auto_ticks.py:


====================================
Automatically setting tick positions
====================================

Setting the behavior of tick auto-placement.

By default, Matplotlib will choose the number of ticks and tick positions so
that there is a reasonable number of ticks on the axis and they are located
at "round" numbers.

As a result, there may be no ticks on the edges of the plot.

.. GENERATED FROM PYTHON SOURCE LINES 14-27

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    np.random.seed(19680801)

    fig, ax = plt.subplots()
    dots = np.linspace(0.3, 1.2, 10)
    X, Y = np.meshgrid(dots, dots)
    x, y = X.ravel(), Y.ravel()
    ax.scatter(x, y, c=x+y)
    plt.show()




.. image-sg:: /gallery/ticks/images/sphx_glr_auto_ticks_001.png
   :alt: auto ticks
   :srcset: /gallery/ticks/images/sphx_glr_auto_ticks_001.png, /gallery/ticks/images/sphx_glr_auto_ticks_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 28-31

If you want to keep ticks at round numbers, and also have ticks at the edges
you can switch :rc:`axes.autolimit_mode` to 'round_numbers'. This expands the
axis limits to the next round number.

.. GENERATED FROM PYTHON SOURCE LINES 31-42

.. code-block:: Python


    plt.rcParams['axes.autolimit_mode'] = 'round_numbers'

    # Note: The limits are calculated at draw-time. Therefore, when using
    # :rc:`axes.autolimit_mode` in a context manager, it is important that
    # the ``show()`` command is within the context.

    fig, ax = plt.subplots()
    ax.scatter(x, y, c=x+y)
    plt.show()




.. image-sg:: /gallery/ticks/images/sphx_glr_auto_ticks_002.png
   :alt: auto ticks
   :srcset: /gallery/ticks/images/sphx_glr_auto_ticks_002.png, /gallery/ticks/images/sphx_glr_auto_ticks_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 43-45

The round numbers autolimit_mode is still respected if you set an additional
margin around the data using `.Axes.set_xmargin` / `.Axes.set_ymargin`:

.. GENERATED FROM PYTHON SOURCE LINES 45-50

.. code-block:: Python


    fig, ax = plt.subplots()
    ax.scatter(x, y, c=x+y)
    ax.set_xmargin(0.8)
    plt.show()



.. image-sg:: /gallery/ticks/images/sphx_glr_auto_ticks_003.png
   :alt: auto ticks
   :srcset: /gallery/ticks/images/sphx_glr_auto_ticks_003.png, /gallery/ticks/images/sphx_glr_auto_ticks_003_2_00x.png 2.00x
   :class: sphx-glr-single-img






.. _sphx_glr_download_gallery_ticks_auto_ticks.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: auto_ticks.ipynb <auto_ticks.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: auto_ticks.py <auto_ticks.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: auto_ticks.zip <auto_ticks.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
