Skip to content

Commit fb5b91e

Browse files
authored
Merge pull request #21874 from StefRe/date_fmt-loc_reference
DOC: Add Date Tick Locators and Formatters example
2 parents 801d1b7 + 2f8c0fe commit fb5b91e

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ per-file-ignores =
120120
examples/subplots_axes_and_figures/demo_constrained_layout.py: E402
121121
examples/text_labels_and_annotations/custom_legends.py: E402
122122
examples/ticks/date_concise_formatter.py: E402
123+
examples/ticks/date_formatters_locators.py: F401
123124
examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py: E402
124125
examples/user_interfaces/embedding_in_gtk3_sgskip.py: E402
125126
examples/user_interfaces/embedding_in_gtk4_panzoom_sgskip.py: E402
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)