Python Matplotlib: Visualize Returns, Volatility Scatter, & Relative Performance | Part 12 📈

Matt Macarty · Beginner ·🛠️ AI Tools & Apps ·4y ago

Key Takeaways

This video demonstrates how to use Python's Matplotlib library to visualize stock returns, volatility scatter, and relative performance. It covers creating scatter plots, line plots, and customizing plot elements such as titles, labels, and axes.

Full Transcript

Okay, this is part 12 in my series on Python for stock analysis and uh you can find links for the code and the other videos in the description. All right, so when we left off in part 11, I had just sort of completed some basic data transformations in my stock class. All right, so we added some columns to a data frame that we uh collected there and then uh I plotted the distribution of returns. All right, so in this video I'm going to continue along with some more plotting that we may want to do. All right, and uh you may want to somehow plot volatility. Okay, so some of this is similar, right? So I'm going to have this title, right? I'm going to have uh the the super title again and uh I'm going to show at the end. So I'm just going to copy that and make a couple of changes. All right, so this will be volatility. All right, of the of the symbol. All right, or the or the returns of the symbol, right? Okay. And then uh how am I going to plot this? Uh I need this start and end again. Okay, I just need those for the title cuz I'm going to make a scatter plot uh of the of the absolute magnitude of that change in standard deviations uh against against the return. All right, so that's just going to be a plot scatter. All right, and then against that uh absolute magnitude, like I said. All right, and then I'll just add a couple of lines into the plot so we can interpret it a little bit. So I'll add a horizontal line. I'll add that at zero. All right, and I'm going to make it red. All right, and I'll make it a dashed line. Okay, and I'll do something similar for Y. Okay, let's take a look at that. And uh so I'll just switch this up and we'll plot volatility now. All right, and then uh yeah, I'll just comment that out so we don't download the data again. And uh let's see what that gets us. All right, so we get sort of a nice scatter plot and we can see, okay, where the the sort of magnitudes fall down here, right? So uh this is a percent change, right? And this is number of standard deviations. All right, so we can see that, oh, uh in the last year we actually haven't had anything beyond, say, three and a half standard deviations. All right, and yeah, we can see everything is pretty much clustered down here uh under one. All right, if you wanted, you could, I guess, put the horizontal line up here and so you can see uh where where you have uh days uh beyond uh two standard deviations, something like that. Okay? All right, so there's our first plot uh and then I'm going to uh plot performance. All right, so the actual price performance. All right, and again, most of the stuff is about the same. I'm going to copy everything here and then just change what I need to change. All right, so the only thing that's changing really is what's getting plotted, so then the title uh and that kind of stuff. All right, so instead of a scatter, I am going to make a line plot here and let me get rid of all this. All right, so what I'm going to do is look at the relative performance here, right? So starting from the first day in the data set, I want to see, okay, how did we do over the life, right? So I'm going to look at the the close. All right, and I'm going to divide that by the close on the first day. Okay, and then I'm going to subtract one so we can turn that into a percentage. All right, and then once I'm done with all that, let's see. I'm going to multiply it by 100 and then reformat it uh on that axis as a percentage. Okay, I don't need a vertical line anymore, but I'll I'll leave the horizontal line in there as a reference. All right, so we can see where we are above or below, right, that starting price. Okay, and then I will add in the formatting to to change it into a percentage from a decimal. All right, so let's get current axis and it's going to be Y axis set major formatter. All right, and and this is where I'm going to use that that percentage ticker that I imported at the beginning. Okay, let's see what that does. Okay, so there is our performance of Apple, right? So we can see about a year ago here we were doing great. All right, and then yeah, in the last few months, not so much. All right, so again, yeah, you you might want to, you know, do things like, you know, set the figure size, make it a little bigger, something like that. All right. All right, but that is essentially the plots that I wanted to show you. All right, it's not an exhaustive list of plots, obviously, uh but at least that's what I want to add for now and I'll leave it up to you to sort of expand on this. All right, but that's going to do it for part 12 and then in part 13, uh we're going to look at some more data transformations. So I hope to see you there.

Original Description

@MattMacarty ## 🐍 Python Matplotlib: Visualize Returns, Volatility Scatter, & Relative Performance | Part 12 Welcome to **Part 12** of the Python Stock Analysis Course! Continuing our work on the custom `Stock` class, this video focuses on adding more complex visualizations using **Matplotlib**. We implement two crucial plotting methods: one for **risk visualization** and one for **price performance**. You will learn how to create a scatter plot to visualize **volatility outliers** and a clean line plot to show the **relative (normalized) performance** of the stock over the entire data range. ### 🎯 Key Learning Outcomes: 1. **Volatility Scatter Plot:** Create a scatter plot to visualize **Daily Returns** (Y-axis) against **Movement Magnitude** (X-axis, measured in standard deviations). This helps identify and visualize volatility outliers and risk. 2. **Relative Performance Plot:** Implement a line plot that visualizes the stock's **Normalized Performance** (percentage gain/loss since the start of the data). 3. **Data Normalization:** Learn the Pandas/NumPy calculation for normalizing the close price: `(Close / First Close) - 1`. 4. **Matplotlib Formatting:** Use the **`PercentFormatter`** from `matplotlib.ticker` to correctly display the Y-axis of the performance plot as clean percentages. ### ⏱️ Video Chapters (Jump Ahead!): 0:00 - Introduction & Review (Continuing with the `Stock` Class) 0:36 - Defining the **`plot_volatility`** Method 1:08 - Creating the **Returns vs. Magnitude Scatter Plot** 1:35 - Adding Reference Lines for Interpretation 1:58 - Testing and Analyzing the Volatility Scatter Plot 2:46 - Defining the **`plot_performance`** Method 3:17 - Calculating **Relative Performance** (Normalization) 4:00 - Formatting the Y-Axis as a **Percentage** 4:18 - Testing and Reviewing the Final Performance Plot 4:55 - Preview of Part 13: More Data Transformations ### 🔗 Course Series & Resources: * **Part 11 (Returns & Volatility Calculation):** [https:
Sign in to unlock AI tutor explanation · ⚡30

This video teaches how to create visualizations for stock analysis using Python's Matplotlib library, covering scatter plots, line plots, and customization. It provides a foundation for further data analysis and visualization.

Key Takeaways
  1. Import necessary libraries
  2. Create a scatter plot for volatility
  3. Create a line plot for relative performance
  4. Customize plot elements such as titles and labels
💡 Customizing plot elements and using different types of plots can help effectively communicate insights from stock data

Related Reads

📰
AI tools for Spanish teachers: a practical 2026 guide
Discover AI tools to enhance Spanish teaching, increasing personalized learning and efficiency
Dev.to AI
📰
10 AI Tools That Save Me 20 Hours Per Week in 2026
Discover 10 AI tools that can save you 20 hours per week by automating repetitive tasks and enhancing productivity
Dev.to AI
📰
The hard part of a personal memory tool isn’t capture — it’s proving the answer
Building a personal memory tool is not just about capturing information, but also about verifying the accuracy of the answers it provides
Medium · AI
📰
El futuro de la automatización SaaS: cómo la IA está redefiniendo la eficiencia empresarial
Aprende cómo la IA está revolucionando la automatización SaaS y mejorando la eficiencia empresarial
Dev.to AI

Chapters (10)

Introduction & Review (Continuing with the `Stock` Class)
0:36 Defining the **`plot_volatility`** Method
1:08 Creating the **Returns vs. Magnitude Scatter Plot**
1:35 Adding Reference Lines for Interpretation
1:58 Testing and Analyzing the Volatility Scatter Plot
2:46 Defining the **`plot_performance`** Method
3:17 Calculating **Relative Performance** (Normalization)
4:00 Formatting the Y-Axis as a **Percentage**
4:18 Testing and Reviewing the Final Performance Plot
4:55 Preview of Part 13: More Data Transformations
Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →