Learn how to visualize Apple's historical stock prices using Python's pyplot library, including methods to pinpoint and plot significant highs and lows. Gain practical experience in identifying and marking critical data points on stock price charts.
Key Insights
- The tutorial demonstrates plotting Apple's historical stock prices by using Python's pyplot with the BMH style, visualizing closing prices over time.
- It explains how to identify and retrieve significant data points, such as the lowest stock price, by using pandas functions like
min()
and indexing techniques. - The article introduces an additional challenge to readers, suggesting the use of pyplot's scatterplot method to highlight both the highest and lowest points on the plotted graph.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
Let's plot some data. First, I'm going to include the low, so I'm going to add that here as well. We'll get rid of the print statements—we don't really need those.
Let's find the lowest price in the data frame. That will be lowest_apple_price = apple_prices["3. low"].min()
. It's very similar to before, just the reverse. This gives us the lowest of the low prices.
Then let's find the row with that price: low_date = apple_prices[apple_prices["3. low"] == lowest_apple_price]
.
And finally, let's actually get the date from that. low_date = low_date.index[0]
. Let's run that block again.
Now that we're graphing, let's just see. I don’t have quite everything here, but if I run this, it’s going to make a big old pyplot
figure using the BMH style. We plot the dates as the X values against the closing prices as the Y values. Then we use pyplot.show()
to display the graph. And here it is.
Here are Apple’s lowest prices in the early 2000s, and then rising, rising, rising. All right, so that's how we can do some good work with this data. Now, I’d like to give you folks an extra challenge.
Use pyplot.scatter()
to add the high and the low to the graph. You can kind of eyeball the high—it looks like it's this special day here in 2012—but it's not entirely clear where the low is.
Probably somewhere in here. I mean, we can look at the date—we can print out the date.
We've got these variables: low_date
and lowest_apple_price
, high_price
and high_date
. We want to take those two X and Y coordinates and plot them using pyplot.scatter()
. That’s our next challenge.
And I'll let you folks do that, and we'll talk about how we do it in the next video. Take a moment and see if you can figure it out on your own.