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

Skip to content

Commit 8d3014d

Browse files
committed
DOC: add example and whats new
1 parent 91c0085 commit 8d3014d

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
New date formatter: `~.dates.ConciseDateFormatter`
2+
--------------------------------------------------
3+
4+
The automatic date formatter used by default can be quite verbose. A new
5+
formatter can be accessed that tries to make the tick labels appropriately
6+
concise.
7+
8+
.. plot::
9+
10+
import matplotlib.pyplot as plt
11+
import matplotlib.dates as mdates
12+
import numpy as np
13+
14+
# make a timeseries...
15+
dt = np.timedelta64(2, 'h', dtype='datetime64[s]')
16+
dates = np.arange('2005-02', '2005-04', dt, dtype='datetime64[s]')
17+
N = len(dates)
18+
19+
np.random.seed(19680801)
20+
y = np.cumsum(np.random.randn(N))
21+
22+
lims = [(np.datetime64('2005-02'), np.datetime64('2005-04')),
23+
(np.datetime64('2005-02-03'), np.datetime64('2005-02-15')),
24+
(np.datetime64('2005-02-03 11:00'), np.datetime64('2005-02-04 13:20'))]
25+
fig, axs = plt.subplots(3, 1, constrained_layout=True)
26+
for nn, ax in enumerate(axs):
27+
# activate the formatter here.
28+
locator = mdates.AutoDateLocator()
29+
formatter = mdates.ConciseDateFormatter(locator)
30+
ax.xaxis.set_major_locator(locator)
31+
ax.xaxis.set_major_formatter(formatter)
32+
33+
ax.plot(dates, y)
34+
ax.set_xlim(lims[nn])
35+
axs[0].set_title('Concise Date Formatter')
36+
37+
plt.show()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
================================================
3+
Formatting date ticks using ConciseDateFormatter
4+
================================================
5+
6+
Finding good tick values and formatting the ticks for an axis that
7+
has date data is often a challenge. `~.dates.ConciseDateFormatter` is
8+
meant to improve the strings chosen at various date intervals.
9+
10+
"""
11+
import datetime
12+
import matplotlib.pyplot as plt
13+
import matplotlib.dates as mdates
14+
import numpy as np
15+
16+
# make a timeseries...
17+
dt = np.timedelta64(2, 'h', dtype='datetime64[s]')
18+
dates = np.arange('2005-02', '2005-04', dt, dtype='datetime64[s]')
19+
N = len(dates)
20+
21+
np.random.seed(19680801)
22+
y = np.cumsum(np.random.randn(N))
23+
24+
fig, axs = plt.subplots(3, 1, constrained_layout=True)
25+
lims = [(np.datetime64('2005-02'), np.datetime64('2005-04')),
26+
(np.datetime64('2005-02-03'), np.datetime64('2005-02-15')),
27+
(np.datetime64('2005-02-03 11:00'), np.datetime64('2005-02-04 13:20'))]
28+
for nn, ax in enumerate(axs):
29+
ax.plot(dates, y)
30+
ax.set_xlim(lims[nn])
31+
# rotate_labels...
32+
for label in ax.get_xticklabels():
33+
label.set_rotation(40)
34+
label.set_horizontalalignment('right')
35+
axs[0].set_title('Default Date Formatter')
36+
plt.show()
37+
38+
#############################################################################
39+
# The default date formater is a bit verbose, but Matplotlib has an
40+
# an alternative that can be chosen with a few extra lines of code. Note,
41+
# for this size axes the labels do not need to be rotated.
42+
43+
fig, axs = plt.subplots(3, 1, constrained_layout=True)
44+
for nn, ax in enumerate(axs):
45+
locator = mdates.AutoDateLocator()
46+
formatter = mdates.ConciseDateFormatter(locator)
47+
ax.xaxis.set_major_locator(locator)
48+
ax.xaxis.set_major_formatter(formatter)
49+
50+
ax.plot(dates, y)
51+
ax.set_xlim(lims[nn])
52+
axs[0].set_title('Concise Date Formatter')
53+
54+
plt.show()

lib/matplotlib/dates.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,14 @@ def __call__(self, x, pos=0):
762762
return num2date(self.t[ind], self.tz).strftime(self.fmt)
763763

764764

765-
class NewDateFormatter(ticker.Formatter):
765+
class ConciseDateFormatter(ticker.Formatter):
766766
"""
767-
This class attempts to figure out the best format to use. This is
767+
This class attempts to figure out the best format to use for the
768+
date, and to make it as compact as possible, but still complete. This is
768769
most useful when used with the :class:`AutoDateLocator`.
769770
770771
>>> locator = AutoDateLocator()
771-
>>> formatter = NewDateFormatter(locator)
772+
>>> formatter = ConciseDateFormatter(locator)
772773
773774
"""
774775

@@ -1525,7 +1526,7 @@ def get_locator(self, dmin, dmax):
15251526
self._freq = freq
15261527

15271528
if self._byranges[i] and self.interval_multiples:
1528-
if i == DAILY and interval == 14:
1529+
if i <= DAILY and interval == 14:
15291530
# just make first and 15th. Avoids 30th.
15301531
byranges[i] = [1, 15]
15311532
else:

0 commit comments

Comments
 (0)