Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfd7e5d

Browse files
committed
DOC: Improve readability of date formatters/locators example
by grouping code and output
1 parent f017315 commit bfd7e5d

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

galleries/examples/ticks/date_formatters_locators.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,6 @@
1818
WeekdayLocator, YearLocator, rrulewrapper)
1919
import matplotlib.ticker as ticker
2020

21-
locators = [
22-
# locator as str, xmax, fmt
23-
('AutoDateLocator(maxticks=8)', '2003-02-01', '%Y-%m'),
24-
('YearLocator(month=4)', '2003-02-01', '%Y-%m'),
25-
('MonthLocator(bymonth=[4, 8, 12])', '2003-02-01', '%Y-%m'),
26-
('DayLocator(interval=180)', '2003-02-01', '%Y-%m-%d'),
27-
('WeekdayLocator(byweekday=SU, interval=4)', '2000-07-01', '%a %Y-%m-%d'),
28-
('HourLocator(byhour=range(0, 24, 6))', '2000-02-04', '%H h'),
29-
('MinuteLocator(interval=15)', '2000-02-01 02:00', '%H:%M'),
30-
('SecondLocator(bysecond=(0, 30))', '2000-02-01 00:02', '%H:%M:%S'),
31-
('MicrosecondLocator(interval=1000)', '2000-02-01 00:00:00.005', '%S.%f'),
32-
('RRuleLocator(rrulewrapper(freq=MONTHLY, \nbyweekday=(MO, TU, WE, TH, FR), '
33-
'bysetpos=-1))', '2000-07-01', '%Y-%m-%d'),
34-
]
35-
36-
formatters = [
37-
'AutoDateFormatter(ax.xaxis.get_major_locator())',
38-
'ConciseDateFormatter(ax.xaxis.get_major_locator())',
39-
'DateFormatter("%b %Y")',
40-
]
41-
4221

4322
def plot_axis(ax, locator=None, xmax='2002-02-01', fmt=None, formatter=None):
4423
"""Set up common parameters for the Axes in the example."""
@@ -55,13 +34,42 @@ def plot_axis(ax, locator=None, xmax='2002-02-01', fmt=None, formatter=None):
5534
ax.text(0.0, 0.2, locator or formatter, transform=ax.transAxes,
5635
fontsize=14, fontname='Monospace', color='tab:blue')
5736

37+
# %%
38+
# :ref:`Date locators <date-tickers>` (also known as tickers)
39+
# -----------------------------------------------------------
40+
41+
42+
locators = [
43+
# locator as str, xmax, fmt
44+
('AutoDateLocator(maxticks=8)', '2003-02-01', '%Y-%m'),
45+
('YearLocator(month=4)', '2003-02-01', '%Y-%m'),
46+
('MonthLocator(bymonth=[4, 8, 12])', '2003-02-01', '%Y-%m'),
47+
('DayLocator(interval=180)', '2003-02-01', '%Y-%m-%d'),
48+
('WeekdayLocator(byweekday=SU, interval=4)', '2000-07-01', '%a %Y-%m-%d'),
49+
('HourLocator(byhour=range(0, 24, 6))', '2000-02-04', '%H h'),
50+
('MinuteLocator(interval=15)', '2000-02-01 02:00', '%H:%M'),
51+
('SecondLocator(bysecond=(0, 30))', '2000-02-01 00:02', '%H:%M:%S'),
52+
('MicrosecondLocator(interval=1000)', '2000-02-01 00:00:00.005', '%S.%f'),
53+
('RRuleLocator(rrulewrapper(freq=MONTHLY, \nbyweekday=(MO, TU, WE, TH, FR), '
54+
'bysetpos=-1))', '2000-07-01', '%Y-%m-%d'),
55+
]
5856

5957
fig, axs = plt.subplots(len(locators), 1, figsize=(8, len(locators) * .8),
6058
layout='constrained')
6159
fig.suptitle('Date Locators')
6260
for ax, (locator, xmax, fmt) in zip(axs, locators):
6361
plot_axis(ax, locator, xmax, fmt)
6462

63+
# %%
64+
# :ref:`date-formatters`
65+
# ----------------------
66+
67+
formatters = [
68+
'AutoDateFormatter(ax.xaxis.get_major_locator())',
69+
'ConciseDateFormatter(ax.xaxis.get_major_locator())',
70+
'DateFormatter("%b %Y")',
71+
]
72+
6573
fig, axs = plt.subplots(len(formatters), 1, figsize=(8, len(formatters) * .8),
6674
layout='constrained')
6775
fig.suptitle('Date Formatters')

lib/matplotlib/dates.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
100100
.. _dateutil: https://dateutil.readthedocs.io
101101
102+
.. _date-tickers:
103+
102104
Date tickers
103105
------------
104106
@@ -154,6 +156,8 @@
154156
4 hours, it will pick hours 0, 4, 8, etc. as ticks. This behaviour is not
155157
guaranteed by default.
156158
159+
.. _date-formatters:
160+
157161
Date formatters
158162
---------------
159163

0 commit comments

Comments
 (0)