|
24 | 24 | '2008-10-13', '2008-10-14'], dtype='datetime64[D]') |
25 | 25 |
|
26 | 26 | The dtype of the NumPy record array for the field ``date`` is ``datetime64[D]`` |
27 | | -which means it is a 64-bit `numpy.datetime64` in 'day' units. While this format |
28 | | -is more portable, Matplotlib cannot plot this format natively yet. We can plot |
29 | | -this data by changing the dates to `datetime.date` instances instead, which can |
30 | | -be achieved by converting to an object array:: |
31 | | -
|
32 | | - In [67]: r.date.astype('O') |
33 | | - array([datetime.date(2004, 8, 19), datetime.date(2004, 8, 20), |
34 | | - datetime.date(2004, 8, 23), ..., datetime.date(2008, 10, 10), |
35 | | - datetime.date(2008, 10, 13), datetime.date(2008, 10, 14)], |
36 | | - dtype=object) |
37 | | -
|
38 | | -The dtype of this converted array is now ``object`` and it is filled with |
39 | | -datetime.date instances instead. |
| 27 | +which means it is a 64-bit `numpy.datetime64` in 'day' units. |
40 | 28 |
|
41 | 29 | If you plot the data, :: |
42 | 30 |
|
43 | | - In [67]: plot(r.date.astype('O'), r.close) |
| 31 | + In [67]: plot(r.date, r.close) |
44 | 32 | Out[67]: [<matplotlib.lines.Line2D object at 0x92a6b6c>] |
45 | 33 |
|
46 | 34 | you will see that the x tick labels are all squashed together. |
|
53 | 41 | with cbook.get_sample_data('goog.npz') as datafile: |
54 | 42 | r = np.load(datafile)['price_data'].view(np.recarray) |
55 | 43 |
|
56 | | -# Matplotlib prefers datetime instead of np.datetime64. |
57 | | -date = r.date.astype('O') |
58 | 44 | fig, ax = plt.subplots() |
59 | | -ax.plot(date, r.close) |
| 45 | +ax.plot(r.date, r.close) |
60 | 46 | ax.set_title('Default date handling can cause overlapping labels') |
61 | 47 |
|
62 | 48 | ############################################################################### |
63 | | -# Another annoyance is that if you hover the mouse over the window and |
64 | | -# look in the lower right corner of the matplotlib toolbar |
65 | | -# (:ref:`navigation-toolbar`) at the x and y coordinates, you see that |
66 | | -# the x locations are formatted the same way the tick labels are, e.g., |
67 | | -# "Dec 2004". |
| 49 | +# Another annoyance is that if you hover the mouse over the window and look in |
| 50 | +# the lower right corner of the Matplotlib toolbar (:ref:`navigation-toolbar`) |
| 51 | +# at the x and y coordinates, you see that the x locations are formatted the |
| 52 | +# same way the tick labels are, e.g., "Dec 2004". |
68 | 53 | # |
69 | | -# What we'd like is for the location in the toolbar to have |
70 | | -# a higher degree of precision, e.g., giving us the exact date out mouse is |
71 | | -# hovering over. To fix the first problem, we can use |
72 | | -# :func:`matplotlib.figure.Figure.autofmt_xdate` and to fix the second |
73 | | -# problem we can use the ``ax.fmt_xdata`` attribute which can be set to |
74 | | -# any function that takes a scalar and returns a string. matplotlib has |
75 | | -# a number of date formatters built in, so we'll use one of those. |
| 54 | +# What we'd like is for the location in the toolbar to have a higher degree of |
| 55 | +# precision, e.g., giving us the exact date out mouse is hovering over. To fix |
| 56 | +# the first problem, we can use `.Figure.autofmt_xdate` and to fix the second |
| 57 | +# problem we can use the ``ax.fmt_xdata`` attribute which can be set to any |
| 58 | +# function that takes a scalar and returns a string. Matplotlib has a number |
| 59 | +# of date formatters built in, so we'll use one of those. |
76 | 60 |
|
77 | 61 | fig, ax = plt.subplots() |
78 | | -ax.plot(date, r.close) |
79 | | - |
80 | | -# rotate and align the tick labels so they look better |
| 62 | +ax.plot(r.date, r.close) |
| 63 | +# Rotate and align the tick labels so they look better. |
81 | 64 | fig.autofmt_xdate() |
82 | | - |
83 | | -# use a more precise date string for the x axis locations in the |
84 | | -# toolbar |
| 65 | +# Use a more precise date string for the x axis locations in the toolbar. |
85 | 66 | ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') |
86 | 67 | ax.set_title('fig.autofmt_xdate fixes the labels') |
87 | 68 |
|
|
0 commit comments