|
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 `dates.date2num` and `dates.num2date`. These can convert between |
| 15 | +`datetime.datetime` objects and `numpy.datetime64` objects. |
16 | 16 | """
|
| 17 | + |
17 | 18 | import datetime
|
18 | 19 | import numpy as np
|
19 | 20 | import matplotlib.pyplot as plt
|
|
29 | 30 | # stores the date as an np.datetime64 with a day unit ('D') in the date column.
|
30 | 31 | with cbook.get_sample_data('goog.npz') as datafile:
|
31 | 32 | 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 | 33 |
|
36 | 34 | fig, ax = plt.subplots()
|
37 |
| -ax.plot(date, r.adj_close) |
38 |
| - |
| 35 | +ax.plot(r.date, r.adj_close) |
39 | 36 |
|
40 | 37 | # format the ticks
|
41 | 38 | ax.xaxis.set_major_locator(years)
|
42 | 39 | ax.xaxis.set_major_formatter(yearsFmt)
|
43 | 40 | ax.xaxis.set_minor_locator(months)
|
44 | 41 |
|
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') |
47 | 45 | ax.set_xlim(datemin, datemax)
|
48 | 46 |
|
49 | 47 |
|
|
0 commit comments