Skip to content

Commit 589bf7a

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

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed
Lines changed: 10 additions & 0 deletions
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. We
5+
now support `numpy.datetime64` dates as well. This should be
6+
transparent to the user, and anywhere that `dateime.datetime` could be
7+
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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
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 `dates.date2num` and `dates.num2date`. These can convert between
15+
`datetime.datetime` objects and `numpy.datetime64` objects.
1616
"""
17+
1718
import datetime
1819
import numpy as np
1920
import matplotlib.pyplot as plt
@@ -29,21 +30,18 @@
2930
# stores the date as an np.datetime64 with a day unit ('D') in the date column.
3031
with cbook.get_sample_data('goog.npz') as datafile:
3132
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')
3533

3634
fig, ax = plt.subplots()
37-
ax.plot(date, r.adj_close)
38-
35+
ax.plot(r.date, r.adj_close)
3936

4037
# format the ticks
4138
ax.xaxis.set_major_locator(years)
4239
ax.xaxis.set_major_formatter(yearsFmt)
4340
ax.xaxis.set_minor_locator(months)
4441

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

4947

0 commit comments

Comments
 (0)