
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/ticks/ticks_too_many.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_ticks_too_many.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_ticks_ticks_too_many.py:


=====================
Fixing too many ticks
=====================

One common cause for unexpected tick behavior is passing a list of strings
instead of numbers or datetime objects. This can easily happen without notice
when reading in a comma-delimited text file. Matplotlib treats lists of strings
as *categorical* variables
(:doc:`/gallery/lines_bars_and_markers/categorical_variables`), and by default
puts one tick per category, and plots them in the order in which they are
supplied.  If this is not desired, the solution is to convert the strings to
a numeric type as in the following examples.

.. GENERATED FROM PYTHON SOURCE LINES 18-20

Example 1: Strings can lead to an unexpected order of number ticks
------------------------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 20-38

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    fig, ax = plt.subplots(1, 2, layout='constrained', figsize=(6, 2.5))
    x = ['1', '5', '2', '3']
    y = [1, 4, 2, 3]
    ax[0].plot(x, y, 'd')
    ax[0].tick_params(axis='x', color='r', labelcolor='r')
    ax[0].set_xlabel('Categories')
    ax[0].set_title('Ticks seem out of order / misplaced')

    # convert to numbers:
    x = np.asarray(x, dtype='float')
    ax[1].plot(x, y, 'd')
    ax[1].set_xlabel('Floats')
    ax[1].set_title('Ticks as expected')




.. image-sg:: /gallery/ticks/images/sphx_glr_ticks_too_many_001.png
   :alt: Ticks seem out of order / misplaced, Ticks as expected
   :srcset: /gallery/ticks/images/sphx_glr_ticks_too_many_001.png, /gallery/ticks/images/sphx_glr_ticks_too_many_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 39-43

Example 2: Strings can lead to very many ticks
----------------------------------------------
If *x* has 100 elements, all strings, then we would have 100 (unreadable)
ticks, and again the solution is to convert the strings to floats:

.. GENERATED FROM PYTHON SOURCE LINES 43-56

.. code-block:: Python


    fig, ax = plt.subplots(1, 2, figsize=(6, 2.5))
    x = [f'{xx}' for xx in np.arange(100)]
    y = np.arange(100)
    ax[0].plot(x, y)
    ax[0].tick_params(axis='x', color='r', labelcolor='r')
    ax[0].set_title('Too many ticks')
    ax[0].set_xlabel('Categories')

    ax[1].plot(np.asarray(x, float), y)
    ax[1].set_title('x converted to numbers')
    ax[1].set_xlabel('Floats')




.. image-sg:: /gallery/ticks/images/sphx_glr_ticks_too_many_002.png
   :alt: Too many ticks, x converted to numbers
   :srcset: /gallery/ticks/images/sphx_glr_ticks_too_many_002.png, /gallery/ticks/images/sphx_glr_ticks_too_many_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 57-62

Example 3: Strings can lead to an unexpected order of datetime ticks
--------------------------------------------------------------------
A common case is when dates are read from a CSV file, they need to be
converted from strings to datetime objects to get the proper date locators
and formatters.

.. GENERATED FROM PYTHON SOURCE LINES 62-77

.. code-block:: Python


    fig, ax = plt.subplots(1, 2, layout='constrained', figsize=(6, 2.75))
    x = ['2021-10-01', '2021-11-02', '2021-12-03', '2021-09-01']
    y = [0, 2, 3, 1]
    ax[0].plot(x, y, 'd')
    ax[0].tick_params(axis='x', labelrotation=90, color='r', labelcolor='r')
    ax[0].set_title('Dates out of order')

    # convert to datetime64
    x = np.asarray(x, dtype='datetime64[s]')
    ax[1].plot(x, y, 'd')
    ax[1].tick_params(axis='x', labelrotation=90)
    ax[1].set_title('x converted to datetimes')

    plt.show()



.. image-sg:: /gallery/ticks/images/sphx_glr_ticks_too_many_003.png
   :alt: Dates out of order, x converted to datetimes
   :srcset: /gallery/ticks/images/sphx_glr_ticks_too_many_003.png, /gallery/ticks/images/sphx_glr_ticks_too_many_003_2_00x.png 2.00x
   :class: sphx-glr-single-img






.. _sphx_glr_download_gallery_ticks_ticks_too_many.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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