Skip to content

Commit fefbd64

Browse files
committed
DOC: for datetime64 support
1 parent 3ccaf2d commit fefbd64

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Support for numpy.datetime64
2+
----------------------------
3+
4+
Matplotlib has supported `datetime.datetime` dates for a long time in
5+
`matplotlib.dates`. We
6+
now support `numpy.datetime64` dates as well. Anywhere that
7+
`dateime.datetime` could be used, `numpy.datetime64` can be used. eg::
8+
9+
time = np.arange('2005-02-01', '2005-02-02', dtype='datetime64[h]')
10+
plt.plot(time)

examples/api/date.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
formatters. See major_minor_demo1.py for more information on
88
controlling major and minor ticks
99
10-
All matplotlib date plotting is done by converting date instances into
11-
days since the 0001-01-01 UTC. The conversion, tick locating and
12-
formatting is done behind the scenes so this is most transparent to
13-
you. The dates module provides several converter functions date2num
14-
and num2date
15-
10+
All matplotlib date plotting is done by converting date instances into days
11+
since 0001-01-01 00:00:00 UTC plus one day (for historical reasons). The
12+
conversion, tick locating and formatting is done behind the scenes so this
13+
is most transparent to you. The dates module provides several converter
14+
functions `matplotlib.dates.date2num` and `matplotlib.dates.num2date`.
15+
These can convert between `datetime.datetime` objects and
16+
`numpy.datetime64` objects.
1617
"""
18+
1719
import datetime
1820
import numpy as np
1921
import matplotlib.pyplot as plt
@@ -29,21 +31,18 @@
2931
# stores the date as an np.datetime64 with a day unit ('D') in the date column.
3032
with cbook.get_sample_data('goog.npz') as datafile:
3133
r = np.load(datafile)['price_data'].view(np.recarray)
32-
# Matplotlib works better with datetime.datetime than np.datetime64, but the
33-
# latter is more portable.
34-
date = r.date.astype('O')
3534

3635
fig, ax = plt.subplots()
37-
ax.plot(date, r.adj_close)
38-
36+
ax.plot(r.date, r.adj_close)
3937

4038
# format the ticks
4139
ax.xaxis.set_major_locator(years)
4240
ax.xaxis.set_major_formatter(yearsFmt)
4341
ax.xaxis.set_minor_locator(months)
4442

45-
datemin = datetime.date(date.min().year, 1, 1)
46-
datemax = datetime.date(date.max().year + 1, 1, 1)
43+
# round to nearest years...
44+
datemin = np.datetime64(r.date[0], 'Y')
45+
datemax = np.datetime64(r.date[-1], 'Y') + np.timedelta64(1, 'Y')
4746
ax.set_xlim(datemin, datemax)
4847

4948

0 commit comments

Comments
 (0)