@@ -87,28 +87,38 @@ gracefully, and here are some tricks to help you work around them.
8787We'll load up some sample date data which contains datetime.date
8888objects in a numpy record array::
8989
90- In [63]: datafile = cbook.get_sample_data('goog.npy ')
90+ In [63]: datafile = cbook.get_sample_data('goog.npz ')
9191
92- In [64]: r = np.load(datafile).view(np.recarray)
92+ In [64]: r = np.load(datafile)['price_data'] .view(np.recarray)
9393
9494 In [65]: r.dtype
95- Out[65]: dtype([('date', '|O4 '), ('', '|V4'), ('open', '<f8'),
95+ Out[65]: dtype([('date', '<M8[D] '), ('', '|V4'), ('open', '<f8'),
9696 ('high', '<f8'), ('low', '<f8'), ('close', '<f8'),
9797 ('volume', '<i8'), ('adj_close', '<f8')])
9898
9999 In [66]: r.date
100100 Out[66]:
101- array([2004-08-19, 2004-08-20, 2004-08-23, ..., 2008-10-10, 2008-10-13 ,
102- 2008-10-14 ], dtype=object )
101+ array([' 2004-08-19', ' 2004-08-20', ' 2004-08-23' , ..., ' 2008-10-10' ,
102+ ' 2008-10-13', '2008-10-14' ], dtype='datetime64[D]' )
103103
104- The dtype of the numpy record array for the field ``date `` is ``|O4 ``
105- which means it is a 4-byte python object pointer; in this case the
106- objects are datetime.date instances, which we can see when we print
107- some samples in the ipython terminal window.
104+ The dtype of the NumPy record array for the field ``date `` is ``datetime64[D] ``
105+ which means it is a 64-bit `np.datetime64 ` in 'day' units. While this format is
106+ more portable, Matplotlib cannot plot this format natively yet. We can plot
107+ this data by changing the dates to `datetime.date ` instances instead, which can
108+ be achieved by converting to an object array::
109+
110+ In [67]: r.date.astype('O')
111+ array([datetime.date(2004, 8, 19), datetime.date(2004, 8, 20),
112+ datetime.date(2004, 8, 23), ..., datetime.date(2008, 10, 10),
113+ datetime.date(2008, 10, 13), datetime.date(2008, 10, 14)],
114+ dtype=object)
115+
116+ The dtype of this converted array is now ``object `` and it is filled with
117+ datetime.date instances instead.
108118
109119If you plot the data, ::
110120
111- In [67]: plot(r.date, r.close)
121+ In [67]: plot(r.date.astype('O') , r.close)
112122 Out[67]: [<matplotlib.lines.Line2D object at 0x92a6b6c>]
113123
114124you will see that the x tick labels are all squashed together.
@@ -117,18 +127,12 @@ you will see that the x tick labels are all squashed together.
117127 :context:
118128
119129 import matplotlib.cbook as cbook
120- datafile = cbook.get_sample_data('goog.npy')
121- try:
122- # Python3 cannot load python2 .npy files with datetime(object) arrays
123- # unless the encoding is set to bytes. Hovever this option was
124- # not added until numpy 1.10 so this example will only work with
125- # python 2 or with numpy 1.10 and later.
126- r = np.load(datafile, encoding='bytes').view(np.recarray)
127- except TypeError:
128- # Old Numpy
129- r = np.load(datafile).view(np.recarray)
130+ with cbook.get_sample_data('goog.npz') as datafile:
131+ r = np.load(datafile)['price_data'].view(np.recarray)
132+ # Matplotlib prefers datetime instead of np.datetime64.
133+ date = r.date.astype('O')
130134 plt.figure()
131- plt.plot(r. date, r.close)
135+ plt.plot(date, r.close)
132136 plt.title('Default date handling can cause overlapping labels')
133137
134138Another annoyance is that if you hover the mouse over the window and
@@ -149,7 +153,7 @@ a number of date formatters built in, so we'll use one of those.
149153
150154 plt.close('all')
151155 fig, ax = plt.subplots(1)
152- ax.plot(r. date, r.close)
156+ ax.plot(date, r.close)
153157
154158 # rotate and align the tick labels so they look better
155159 fig.autofmt_xdate()
@@ -186,22 +190,17 @@ right.
186190 import matplotlib.cbook as cbook
187191
188192 # load up some sample financial data
189- datafile = cbook.get_sample_data('goog.npy')
190- try:
191- # Python3 cannot load python2 .npy files with datetime(object) arrays
192- # unless the encoding is set to bytes. Hovever this option was
193- # not added until numpy 1.10 so this example will only work with
194- # python 2 or with numpy 1.10 and later.
195- r = np.load(datafile, encoding='bytes').view(np.recarray)
196- except TypeError:
197- r = np.load(datafile).view(np.recarray)
193+ with cbook.get_sample_data('goog.npz') as datafile:
194+ r = np.load(datafile)['price_data'].view(np.recarray)
195+ # Matplotlib prefers datetime instead of np.datetime64.
196+ date = r.date.astype('O')
198197 # create two subplots with the shared x and y axes
199198 fig, (ax1, ax2) = plt.subplots(1,2, sharex=True, sharey=True)
200199
201200 pricemin = r.close.min()
202201
203- ax1.plot(r. date, r.close, lw=2)
204- ax2.fill_between(r. date, pricemin, r.close, facecolor='blue', alpha=0.5)
202+ ax1.plot(date, r.close, lw=2)
203+ ax2.fill_between(date, pricemin, r.close, facecolor='blue', alpha=0.5)
205204
206205 for ax in ax1, ax2:
207206 ax.grid(True)
0 commit comments