The original code
SPY_price = SPY['price']
simply starts with a series of prices for SPY. The index values are the minute timestamps. The data values are the prices. We want the minutes to be on the x axis and the price values to be on the y axis. However, pyplot is sometimes too smart. It knows that the x axis is a time so it tries to make all the minutes equal-distant. This is what causes the big gaps during non-trading times. The work around is to simply number the x axis with integers (lose the times). That's what this code does
That simply makes a list of integers from 0 to the length of the data we have. To plot we simply give the plot method a list of x values and a list of y values. In this case the range of integers for x and the prices for y.
However, as you noticed we now have the x tick labels as simply integers. You want to put the times back in. The x tick labels can be over-written with any string values that are desired. Use the '.xticks' method to set the labels. The format is a series of positions along the x axis, followed by the corresponding desired labels.
Try adding this line
That will change all the x labels to the string value of the index (ie the datetime). Moreover, with some fancy slicing, it only places a label every 390 ticks. This is simply to make the scale more readable. There are 390 minutes in a typical trading day so it puts a label every day.
See attached notebook.