|
7 | 7 | formatters. See major_minor_demo1.py for more information on |
8 | 8 | controlling major and minor ticks |
9 | 9 |
|
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 | +:class:`numpy.datetime64` objects. |
16 | 17 | """ |
| 18 | + |
17 | 19 | import datetime |
18 | 20 | import numpy as np |
19 | 21 | import matplotlib.pyplot as plt |
|
29 | 31 | # stores the date as an np.datetime64 with a day unit ('D') in the date column. |
30 | 32 | with cbook.get_sample_data('goog.npz') as datafile: |
31 | 33 | 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') |
35 | 34 |
|
36 | 35 | fig, ax = plt.subplots() |
37 | | -ax.plot(date, r.adj_close) |
38 | | - |
| 36 | +ax.plot(r.date, r.adj_close) |
39 | 37 |
|
40 | 38 | # format the ticks |
41 | 39 | ax.xaxis.set_major_locator(years) |
42 | 40 | ax.xaxis.set_major_formatter(yearsFmt) |
43 | 41 | ax.xaxis.set_minor_locator(months) |
44 | 42 |
|
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') |
47 | 46 | ax.set_xlim(datemin, datemax) |
48 | 47 |
|
49 | 48 |
|
|
0 commit comments