2525# low, close, volume, adj_close from the mpl-data/sample_data directory. The
2626# record array stores the date as an np.datetime64 with a day unit ('D') in
2727# the date column (``r.date``).
28- r = cbook .get_sample_data ('goog.npz' )['price_data' ]. view ( np . recarray )
28+ r = cbook .get_sample_data ('goog.npz' )['price_data' ]
2929r = r [:9 ] # get the first 9 days
3030
3131fig , (ax1 , ax2 ) = plt .subplots (nrows = 2 , figsize = (6 , 6 ), layout = 'constrained' )
3232fig .get_layout_engine ().set (hspace = 0.15 )
3333
3434# First we'll do it the default way, with gaps on weekends
35- ax1 .plot (r . date , r . adj_close , 'o-' )
35+ ax1 .plot (r [ " date" ] , r [ " adj_close" ] , 'o-' )
3636
3737# Highlight gaps in daily data
38- gaps = np .flatnonzero (np .diff (r . date ) > np .timedelta64 (1 , 'D' ))
38+ gaps = np .flatnonzero (np .diff (r [ " date" ] ) > np .timedelta64 (1 , 'D' ))
3939for gap in r [['date' , 'adj_close' ]][np .stack ((gaps , gaps + 1 )).T ]:
4040 ax1 .plot (gap .date , gap .adj_close , 'w--' , lw = 2 )
4141ax1 .legend (handles = [ml .Line2D ([], [], ls = '--' , label = 'Gaps in daily data' )])
5151def format_date (x , _ ):
5252 try :
5353 # convert datetime64 to datetime, and use datetime's strftime:
54- return r . date [round (x )].item ().strftime ('%a' )
54+ return r [ " date" ] [round (x )].item ().strftime ('%a' )
5555 except IndexError :
5656 pass
5757
5858# Create an index plot (x defaults to range(len(y)) if omitted)
59- ax2 .plot (r . adj_close , 'o-' )
59+ ax2 .plot (r [ " adj_close" ] , 'o-' )
6060
6161ax2 .set_title ("Plot y at Index Coordinates Using Custom Formatter" )
6262ax2 .xaxis .set_major_formatter (format_date ) # internally creates FuncFormatter
@@ -79,6 +79,6 @@ def __call__(self, x, pos=0):
7979 pass
8080
8181
82- ax2 .xaxis .set_major_formatter (MyFormatter (r . date , '%a' ))
82+ ax2 .xaxis .set_major_formatter (MyFormatter (r [ " date" ] , '%a' ))
8383
8484plt .show ()
0 commit comments