Visualizing Apple Stock Trends with Pyplot: Identifying Highs and Lows

Plot Apple's highest and lowest prices using pyplot's scatterplot method.

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram