{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Fixing too many ticks\n\nOne common cause for unexpected tick behavior is passing a list of strings\ninstead of numbers or datetime objects. This can easily happen without notice\nwhen reading in a comma-delimited text file. Matplotlib treats lists of strings\nas *categorical* variables\n(:doc:`/gallery/lines_bars_and_markers/categorical_variables`), and by default\nputs one tick per category, and plots them in the order in which they are\nsupplied. If this is not desired, the solution is to convert the strings to\na numeric type as in the following examples.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1: Strings can lead to an unexpected order of number ticks\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfig, ax = plt.subplots(1, 2, layout='constrained', figsize=(6, 2.5))\nx = ['1', '5', '2', '3']\ny = [1, 4, 2, 3]\nax[0].plot(x, y, 'd')\nax[0].tick_params(axis='x', color='r', labelcolor='r')\nax[0].set_xlabel('Categories')\nax[0].set_title('Ticks seem out of order / misplaced')\n\n# convert to numbers:\nx = np.asarray(x, dtype='float')\nax[1].plot(x, y, 'd')\nax[1].set_xlabel('Floats')\nax[1].set_title('Ticks as expected')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 2: Strings can lead to very many ticks\nIf *x* has 100 elements, all strings, then we would have 100 (unreadable)\nticks, and again the solution is to convert the strings to floats:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(1, 2, figsize=(6, 2.5))\nx = [f'{xx}' for xx in np.arange(100)]\ny = np.arange(100)\nax[0].plot(x, y)\nax[0].tick_params(axis='x', color='r', labelcolor='r')\nax[0].set_title('Too many ticks')\nax[0].set_xlabel('Categories')\n\nax[1].plot(np.asarray(x, float), y)\nax[1].set_title('x converted to numbers')\nax[1].set_xlabel('Floats')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 3: Strings can lead to an unexpected order of datetime ticks\nA common case is when dates are read from a CSV file, they need to be\nconverted from strings to datetime objects to get the proper date locators\nand formatters.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(1, 2, layout='constrained', figsize=(6, 2.75))\nx = ['2021-10-01', '2021-11-02', '2021-12-03', '2021-09-01']\ny = [0, 2, 3, 1]\nax[0].plot(x, y, 'd')\nax[0].tick_params(axis='x', labelrotation=90, color='r', labelcolor='r')\nax[0].set_title('Dates out of order')\n\n# convert to datetime64\nx = np.asarray(x, dtype='datetime64[s]')\nax[1].plot(x, y, 'd')\nax[1].tick_params(axis='x', labelrotation=90)\nax[1].set_title('x converted to datetimes')\n\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 }