|
| 1 | +""" |
| 2 | +================================= |
| 3 | +Date tick locators and formatters |
| 4 | +================================= |
| 5 | +
|
| 6 | +This example illustrates the usage and effect of the various date locators and |
| 7 | +formatters. |
| 8 | +""" |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +import matplotlib.ticker as ticker |
| 13 | +from matplotlib.dates import (AutoDateLocator, YearLocator, MonthLocator, |
| 14 | + DayLocator, WeekdayLocator, HourLocator, |
| 15 | + MinuteLocator, SecondLocator, MicrosecondLocator, |
| 16 | + RRuleLocator, rrulewrapper, MONTHLY, |
| 17 | + MO, TU, WE, TH, FR, SA, SU, DateFormatter, |
| 18 | + AutoDateFormatter, ConciseDateFormatter) |
| 19 | + |
| 20 | +locators = [ |
| 21 | + ('AutoDateLocator(maxticks=8)', '2003-02-01', '%Y-%m'), |
| 22 | + ('YearLocator(month=4)', '2003-02-01', '%Y-%m'), |
| 23 | + ('MonthLocator(bymonth=[4,8,12])', '2003-02-01', '%Y-%m'), |
| 24 | + ('DayLocator(interval=180)', '2003-02-01', '%Y-%m-%d'), |
| 25 | + ('WeekdayLocator(byweekday=SU, interval=4)', '2000-07-01', '%a %Y-%m-%d'), |
| 26 | + ('HourLocator(byhour=range(0,24,6))', '2000-02-04', '%H h'), |
| 27 | + ('MinuteLocator(interval=15)', '2000-02-01 02:00', '%H:%M'), |
| 28 | + ('SecondLocator(bysecond=(0,30))', '2000-02-01 00:02', '%H:%M:%S'), |
| 29 | + ('MicrosecondLocator(interval=1000)', '2000-02-01 00:00:00.005', '%S.%f'), |
| 30 | + ('RRuleLocator(rrulewrapper(freq=MONTHLY, \nbyweekday=(MO, TU, WE, TH,' + |
| 31 | + ' FR), bysetpos=-1))', '2000-07-01', '%Y-%m-%d') |
| 32 | +] |
| 33 | + |
| 34 | +formatters = [ |
| 35 | + ('AutoDateFormatter(ax.xaxis.get_major_locator())'), |
| 36 | + ('ConciseDateFormatter(ax.xaxis.get_major_locator())'), |
| 37 | + ('DateFormatter("%b %Y")') |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +def plot_axis(ax, locator=None, xmax='2002-02-01', fmt=None, formatter=None): |
| 42 | + """Set up common parameters for the Axes in the example.""" |
| 43 | + ax.spines.right.set_visible(False) |
| 44 | + ax.spines.left.set_visible(False) |
| 45 | + ax.spines.top.set_visible(False) |
| 46 | + ax.yaxis.set_major_locator(ticker.NullLocator()) |
| 47 | + ax.tick_params(which='major', width=1.00, length=5) |
| 48 | + ax.tick_params(which='minor', width=0.75, length=2.5) |
| 49 | + ax.set_xlim(np.datetime64('2000-02-01'), np.datetime64(xmax)) |
| 50 | + if locator: |
| 51 | + ax.xaxis.set_major_locator(eval(locator)) |
| 52 | + ax.xaxis.set_major_formatter(DateFormatter(fmt)) |
| 53 | + else: |
| 54 | + ax.xaxis.set_major_formatter(eval(formatter)) |
| 55 | + ax.text(0.0, 0.2, locator or formatter, transform=ax.transAxes, |
| 56 | + fontsize=14, fontname='Monospace', color='tab:blue') |
| 57 | + |
| 58 | + |
| 59 | +fig, ax = plt.subplots(len(locators), 1, figsize=(8, len(locators) * .8), |
| 60 | + layout='constrained') |
| 61 | +fig.suptitle('Date Locators') |
| 62 | +for i, loc in enumerate(locators): |
| 63 | + plot_axis(ax[i], *loc) |
| 64 | + |
| 65 | +fig, ax = plt.subplots(len(formatters), 1, figsize=(8, len(formatters) * .8), |
| 66 | + layout='constrained') |
| 67 | +fig.suptitle('Date Formatters') |
| 68 | +for i, fmt in enumerate(formatters): |
| 69 | + plot_axis(ax[i], formatter=fmt) |
| 70 | + |
| 71 | + |
| 72 | +############################################################################# |
| 73 | +# |
| 74 | +# .. admonition:: References |
| 75 | +# |
| 76 | +# The use of the following functions, methods, classes and modules is shown |
| 77 | +# in this example: |
| 78 | +# |
| 79 | +# - `matplotlib.dates.AutoDateLocator` |
| 80 | +# - `matplotlib.dates.YearLocator` |
| 81 | +# - `matplotlib.dates.MonthLocator` |
| 82 | +# - `matplotlib.dates.DayLocator` |
| 83 | +# - `matplotlib.dates.WeekdayLocator` |
| 84 | +# - `matplotlib.dates.HourLocator` |
| 85 | +# - `matplotlib.dates.MinuteLocator` |
| 86 | +# - `matplotlib.dates.SecondLocator` |
| 87 | +# - `matplotlib.dates.MicrosecondLocator` |
| 88 | +# - `matplotlib.dates.RRuleLocator` |
| 89 | +# - `matplotlib.dates.rrulewrapper` |
| 90 | +# - `matplotlib.dates.DateFormatter` |
| 91 | +# - `matplotlib.dates.AutoDateFormatter` |
| 92 | +# - `matplotlib.dates.ConciseDateFormatter` |
0 commit comments