{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Date tick locators and formatters\n\nThis example illustrates the usage and effect of the various date locators and\nformatters.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\n# While these appear unused directly, they are used from eval'd strings.\nfrom matplotlib.dates import (FR, MO, MONTHLY, SA, SU, TH, TU, WE, AutoDateFormatter,\n                              AutoDateLocator, ConciseDateFormatter, DateFormatter,\n                              DayLocator, HourLocator, MicrosecondLocator,\n                              MinuteLocator, MonthLocator, RRuleLocator, SecondLocator,\n                              WeekdayLocator, YearLocator, rrulewrapper)\nimport matplotlib.ticker as ticker\n\n\ndef plot_axis(ax, locator=None, xmax='2002-02-01', fmt=None, formatter=None):\n    \"\"\"Set up common parameters for the Axes in the example.\"\"\"\n    ax.spines[['left', 'right', 'top']].set_visible(False)\n    ax.yaxis.set_major_locator(ticker.NullLocator())\n    ax.tick_params(which='major', width=1.00, length=5)\n    ax.tick_params(which='minor', width=0.75, length=2.5)\n    ax.set_xlim(np.datetime64('2000-02-01'), np.datetime64(xmax))\n    if locator:\n        ax.xaxis.set_major_locator(eval(locator))\n        ax.xaxis.set_major_formatter(DateFormatter(fmt))\n    else:\n        ax.xaxis.set_major_formatter(eval(formatter))\n    ax.text(0.0, 0.2, locator or formatter, transform=ax.transAxes,\n            fontsize=14, fontname='Monospace', color='tab:blue')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## `date-locators`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "locators = [\n    # locator as str, xmax, fmt\n    ('AutoDateLocator(maxticks=8)', '2003-02-01', '%Y-%m'),\n    ('YearLocator(month=4)', '2003-02-01', '%Y-%m'),\n    ('MonthLocator(bymonth=[4, 8, 12])', '2003-02-01', '%Y-%m'),\n    ('DayLocator(interval=180)', '2003-02-01', '%Y-%m-%d'),\n    ('WeekdayLocator(byweekday=SU, interval=4)', '2000-07-01', '%a %Y-%m-%d'),\n    ('HourLocator(byhour=range(0, 24, 6))', '2000-02-04', '%H h'),\n    ('MinuteLocator(interval=15)', '2000-02-01 02:00', '%H:%M'),\n    ('SecondLocator(bysecond=(0, 30))', '2000-02-01 00:02', '%H:%M:%S'),\n    ('MicrosecondLocator(interval=1000)', '2000-02-01 00:00:00.005', '%S.%f'),\n    ('RRuleLocator(rrulewrapper(freq=MONTHLY, \\nbyweekday=(MO, TU, WE, TH, FR), '\n     'bysetpos=-1))', '2000-07-01', '%Y-%m-%d'),\n]\n\nfig, axs = plt.subplots(len(locators), 1, figsize=(8, len(locators) * .8),\n                        layout='constrained')\nfig.suptitle('Date Locators')\nfor ax, (locator, xmax, fmt) in zip(axs, locators):\n    plot_axis(ax, locator, xmax, fmt)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## `date-formatters`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "formatters = [\n    'AutoDateFormatter(ax.xaxis.get_major_locator())',\n    'ConciseDateFormatter(ax.xaxis.get_major_locator())',\n    'DateFormatter(\"%b %Y\")',\n]\n\nfig, axs = plt.subplots(len(formatters), 1, figsize=(8, len(formatters) * .8),\n                        layout='constrained')\nfig.suptitle('Date Formatters')\nfor ax, fmt in zip(axs, formatters):\n    plot_axis(ax, formatter=fmt)"
      ]
    },
    {
      "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.dates.AutoDateLocator`\n   - `matplotlib.dates.YearLocator`\n   - `matplotlib.dates.MonthLocator`\n   - `matplotlib.dates.DayLocator`\n   - `matplotlib.dates.WeekdayLocator`\n   - `matplotlib.dates.HourLocator`\n   - `matplotlib.dates.MinuteLocator`\n   - `matplotlib.dates.SecondLocator`\n   - `matplotlib.dates.MicrosecondLocator`\n   - `matplotlib.dates.RRuleLocator`\n   - `matplotlib.dates.rrulewrapper`\n   - `matplotlib.dates.DateFormatter`\n   - `matplotlib.dates.AutoDateFormatter`\n   - `matplotlib.dates.ConciseDateFormatter`\n\n.. tags:: component: ticks, purpose: reference\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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}